%% EXTRACT PREDATT DATA 
% Load all the subject files and create a mega array with all trial info
% and save as csv for both the main & memory task
%
% Alexandra Vlassova
% 4 February, 2020

clear all;

%% EXTRACT DATA
s = what(pwd);                                                              % get list of everything in the folder
totalFiles = size(s.mat);                                                   % count how many data files there are

for loadMat = 1:totalFiles(1) 
    load(char(s.mat(loadMat)))                                              % load the data .mat file
     
    %%___ get main task data ___%%
    aa=0;
    for block = 1:2
        for trial = 1:stim.search
            aa = aa+1;
            
            % Get trial & block
            f.subject(aa)           = subject;                              % get subject number
            f.trial(aa)             = aa;                                   % get trial number
            f.block(aa)             = block;                                % get block number
            
            % Get counterbalancing info
            f.structStream(aa)      = data.chosen.strStream;                % which shapes are in the structured stream
            f.structLocation(aa)    = data.chosen.strLocation;              % get location of structured stream
            
            % Get trial info
            f.searchTrials(aa)      = data.search(block).trials(trial);     % Get trial number of visual search trials 
            f.searchRotation(aa)    = data.search(block).orientation(trial);% Get which way target was rotated 
            f.searchLocation(aa)    = data.search(block).location(trial);   % Get which location target was in
            
            % Get data
            f.responses(aa)         = data.responses(block,trial);          % Get rotation response
            f.RT(aa)                = data.RT(block,trial);                 % Get RT of rotation response
        end
    end
     
    saveSubs{:,:,loadMat} = [f.subject; f.trial; f.block; f.structStream; f.structLocation;...
        f.searchTrials; f.searchRotation; f.searchLocation; f.responses; f.RT]';
     
    %%___ get mem task data ___%%
     
    for mm = 1:mem.trials
        fm.subject(mm)           = subject;                                 % get subject number
        fm.trial(mm)             = mm;                                      % get trial number
        fm.triporder(mm)         = mem.order(mm);                           % 1=triplet presented first, 2=second
        fm.responses(mm)         = mem.responses(mm);                       % response re: triplet oder
        fm.RT(mm)                = mem.RT(mm);                              % RT of response
    end
    
    saveMem{:,:,loadMat} = [fm.subject; fm.trial; fm.triporder; fm.responses; fm.RT]';
     
end

% restructure matrices for saving
saveArray = []; 
for i=1:size(saveSubs,3)
    saveArray = [saveArray; saveSubs{:,:,i}];
end

memArray = [];
for i=1:size(saveMem,3)
    memArray = [memArray; saveMem{:,:,i}];
end

 
%% SAVE TO CSV 
%%___ MAIN ___%%
cHeader = {'subject' 'trial' 'block' 'structured_stream' 'structured_location' 'search_trials' 'search_rotation' 'search_location' 'response' 'RT'}; % column headers
textHeader = strjoin(cHeader, ',');
fid = fopen('main_task.csv','w'); 
fprintf(fid,'%s\n',textHeader);
fclose(fid); 
dlmwrite('main_task.csv',saveArray,'-append');

%%___ MEM ___%%
cHeader = {'subject' 'trial' 'position' 'response' 'RT'}; % column headers
textHeader = strjoin(cHeader, ',');
fid = fopen('memory_test.csv','w'); 
fprintf(fid,'%s\n',textHeader);
fclose(fid); 
dlmwrite('memory_test.csv',memArray,'-append');
disp('saved.')

 