% create_study 
% -----------------
% Curso HEP Granada 2018
% Pandelis Perakakis, Luis Ciria
% peraka@ugr.es, lciria@ugr.es
% ------------------------------
% 
% initialize the study structure
[ALLEEG EEG CURRENTSET ALLCOM] = eeglab; % open eeglab
STUDY = []; CURRENTSTUDY = 0; ALLEEG=[]; EEG=[]; CURRENTSET=[];
%
% define variables
studyname = 'HEP_study'; 
savename = 'HEP.study';
taskname = 'PVT';
studypath = cd;
groups = {'HF' 'LF'}; % cell variable with at least one element
subjects = 1:5; % vector of subjects to analyse 
sessions = {'PVT'}; % cell variable with at least one element 
conditions = {'HEP2'};
% check if ismac
if ismac == 1
    separator = '/';
else
    separator = '\';
end
%
ind = 1; %study item index
% loop construct the study
for group = 1:length(groups);
    cd ([studypath separator groups{group}]) % enter group folder
    %
    for subject = 1:length(subjects);
        cd (['s' int2str(subjects(subject))]); % enter subject folder
        %
        for session = 1:length(sessions);
            cd (sessions{session}) % enter session folder
            %
            for condition = 1:length(conditions) % get epochs             
                dataset = dir(['*' conditions{condition} '.set']);
                dataset = dataset.name;
                %
                % update the study structure
                [STUDY ALLEEG] = std_editset( STUDY, ALLEEG,...
                    'commands',{ 'index',ind, 'load', [cd separator dataset],...
                    'subject', ['s' int2str(subjects(subject))],...
                    'session', session,...
                    'group', groups{group},...
                    'condition',conditions{condition}});
                %
                ind = ind+1; % increase study item index
            end
            cd ..            
        end
        cd ..
    end
    cd ..
end

% name the study
[STUDY ALLEEG] = std_editset( STUDY, ALLEEG,  'name',...
studyname, 'task', taskname);
%
% update the GUI:
CURRENTSTUDY = 1; EEG = ALLEEG; CURRENTSET = 1:length(EEG);
[STUDY, ALLEEG] = std_checkset(STUDY, ALLEEG);eeglab redraw
%
% save the study structure
[STUDY EEG] = pop_savestudy( STUDY, EEG,'filename', savename ,...
  'filepath', cd);