%% example 1

sub = {'01', '02', '03', '04', '05', '06', '07', '08', '09', '10'};

% for subject 3 the age is unknown, for subject 2 the sex is not specified
age = [11  96  nan 77  82  87  18 40  26  80];
sex = {'f' [] 'f' 'f' 'f' 'm' 'm' 'm' 'm' 'm'};

for subindx=1:numel(sub)
  
  cfg = [];
  cfg.method    = 'copy';
  cfg.datatype  = 'eeg';
  
  % specify the input file name, here we are using the same file for every subject
  cfg.dataset   = './original/Eeglab_data.set';
  
  % specify the output directory
  cfg.bidsroot  = 'bids_example1';
  cfg.sub       = sub{subindx};
  
  % specify the information for the participants.tsv file
  % this is optional, you can also pass other pieces of info
  cfg.participants.age = age(subindx);
  cfg.participants.sex = sex{subindx};
  
  % specify the information for the scans.tsv file
  % this is optional, you can also pass other pieces of info
  cfg.scans.acq_time = datestr(now, 'yyyy-mm-ddThh:MM:SS'); % according to RFC3339
  
  % specify some general information that will be added to the eeg.json file
  cfg.InstitutionName             = 'University of California San Diego';
  cfg.InstitutionalDepartmentName = 'Schwartz Center for Computational Neuroscience';
  cfg.InstitutionAddress          = '9500 Gilman Drive # 0559; La Jolla CA 92093, USA';
  
  % provide the mnemonic and long description of the task
  cfg.TaskName        = 'changedetection';
  cfg.TaskDescription = 'Subjects were responding as fast as possible upon a change in a visually presented stimulus.';
  
  % these are EEG specific
  cfg.eeg.PowerLineFrequency = 60;   % since recorded in the USA
  cfg.eeg.EEGReference       = 'M1'; % actually I do not know, but let's assume it was left mastoid
  
  data2bids(cfg);
  
end

%% example 2

sub = {'01', '02', '03', '04', '05', '06', '07', '08', '09', '10'};
ses = {'before', 'after'};
run = [1 2 3]; % this must be numeric

for subindx=1:numel(sub)
  for sesindx=1:numel(ses)
    for runindx=1:numel(run)

      cfg = [];
      cfg.method    = 'convert';
      cfg.datatype  = 'eeg';

      % specify the input file name, here we are using the same file for every subject
      cfg.dataset   = './original/Eeglab_data.set';

      % specify the output directory
      cfg.bidsroot  = 'bids_example2';
      cfg.sub       = sub{subindx};
      cfg.ses       = ses{sesindx};
      cfg.run       = run(runindx);

      data2bids(cfg);

    end % for run
  end % for ses
end % for sub

