% read a single data file and return data and
% channel labels
% Arnaud Delorme - Jan 26, 2010

function [chanlocs data cond group] = readdatafileml(filename);

fid = fopen(filename);
if fid == 1
    error('Cannot open file');
end;
str = fgetl(fid); group = str(3:4);
fgetl(fid);
fgetl(fid);
str = fgetl(fid);
commapos = find(str == ',');
cond = str(3:commapos(1)-2);

C = textscan(fid, '%s%s%s%n');
fclose(fid);
nchans = length(unique(C{2}));
npoints = length(C{4})/nchans;

if round(npoints) ~= npoints
    error('Non-uniform number of points per channel');
end;
    
data = reshape(C{4}, [npoints nchans])';
data(:,1) = [];
chanlocs = C{2}(1:npoints:length(C{2}));

