% read all trials for a given subject
% and return an EEGLAB dataset
% this asumes all the trial of a given subject have been decompressed
% 
% Arnaud Delorme - Jan 26, 2010

function EEG = readdataml(foldername);

EEG = eeg_emptyset;

allfiles = dir(fullfile(foldername, [ foldername(1:2) '*' ]));
allfiles = { allfiles.name };

latency = 1;
fprintf('Reading all files for "%s"\n', foldername);
for index = 1:length(allfiles)
    fprintf('.');
    
    filename = fullfile(foldername, allfiles{index});
    [chanlocs data cond] = readdatafileml(filename);
    if index == 1
        alldata = zeros(size(data,1), size(data,2), length(allfiles));
    end;
    alldata(:,:,index) = data;
    EEG.event(index).type = cond;
    EEG.event(index).latency = latency;
    EEG.event(index).epoch   = index;
    latency = latency + 256;
    
end;
fprintf('\n');

EEG.data    = alldata;
EEG.group   = foldername(1:2);
EEG.subject = foldername;
EEG.chanlocs = struct('labels', chanlocs);
EEG.srate    = 256;
EEG = eeg_checkset(EEG);
