eeglab;
close;

subject = 's01';
%subject = 's02';

% process all files sequentially
% ------------------------------
count = 1;
for ses = 1:6
    for cond = 1:5
        fprintf('\n\n************************\n');
        fprintf('Session %d; Condition %d\n', ses, cond);
        fprintf('************************\n\n');
        EEG = pop_biosig(sprintf('../%sraw/%s_%d_%d.bdf', subject, subject, ses, cond), 'ref', 135);
        %EEG = pop_readbdf(sprintf('../eeg/jo%d_%d.bdf', ses, cond), [ ] ,257, 135); % E7 reference
        EEG = pop_importpres(EEG,  sprintf('../%sraw/%s_%d_%d.log', subject, subject, ses, cond), 'Event Type', 'Time', -1, 'optimalign', 'off');
        
        % merge datasets
        % --------------
        if count == 1
            ALLEEG = eeg_store(ALLEEG, EEG, 1);
        else
            ALLEEG = eeg_store(ALLEEG, EEG, 2);            
            ALLEEG = pop_mergeset( ALLEEG, [1 2], 0);
        end
        count = count + 1;
        
        clear EEG;
    end
end

EEG =ALLEEG(1);
eeglab redraw;
pop_saveset(EEG, 'filename', [ '../' subject '.set' ]);