%Compile data from all machines in one array
%assuming all dataMaster files have been moved to ./data/ 
d = []; %complete dataset, to be assembled
a = what('data'); %list of files in data folder
for i=1:length(a.mat)
    if length(a.mat{i}) >= 10
        if isequal(a.mat{i}(1:10),'dataMaster') %this is a master file for some machine
            load(['data/' a.mat{i}]) %holds a data array with same name as file
            eval(['d = [d ' a.mat{i}(1:end-4) '];']) %append this data array to the full array
        end
    end
end

%Write data to text file
outfile = fopen('data/fullData.txt','w'); %open text file for writing data
fprintf(outfile,'newsub\tsub\tpaid\tcond\ttrial\tstim\ttarget\tresp\trt\n'); %header row (\t is tab, \n is line break)
for s = 1:length(d) %loop through subjects in d
    if d(s).aborted == 0 %skip subjects who didn't finish
        for t = 1:length(d(s).stim) %loop trials
            fprintf(outfile,'%d\t%d\t%d\t%d\t%d\t%d\t%d\t%d\t%f\n',s,d(s).sub,d(s).paid,mod(d(s).sub-1,72)+1,t,d(s).stim(t),d(s).map(d(s).stim(t)),d(s).resp(t),d(s).RT(t));
        end
    end
end
fclose(outfile);