clear
clc

d=dir('*.csv')

%get each subject's data by subject id matrix(:, 2), then create new .csv
%with just the matrix from 1 subject



for i = 1:length({d.name})
    format longg
    dat = d.name;
    data = readtable(d(i).name,opts);
    dataSort = sortrows(data,2);
    %matrix sizes = 144x14
    if dat(1:3) == 'mul' %trials
        frontInc = 1;
        backInc = 144;
        
        numSubs = numel(unique(dataSort(:,2)))
        
        
        for k = 1:numSubs
            newMat = dataSort(frontInc:backInc,:)
            writetable(newMat, strcat('sub', num2str(k), '.csv'))
            frontInc = frontInc + 144 % this incrementing isn't ideal, maybe improve this using unique later
            backInc = backInc + 144
        end
    
    
    
    
    
    
    elseif dat(1:3) == 'pre' %ratings
        frontIncpre = 1;
        backIncpre = 4;
        
        numSubs = numel(unique(dataSort(:,2)))
        
        
        for k = 1:numSubs
            newMat = dataSort(frontIncpre:backIncpre,:)
            writetable(newMat, strcat('sub', num2str(k), 'prerating.csv'))
            frontIncpre = frontIncpre + 4 % this incrementing isn't ideal, maybe improve this using unique later
            backIncpre = backIncpre + 4
        end
    
    
    
    
    
    
    
    elseif dat(1:3) == 'pos' %ratings
        frontIncpost = 1;
        backIncpost = 4;
        
        numSubs = numel(unique(dataSort(:,2)))
        
        
        for k = 1:numSubs
            newMat = dataSort(frontIncpost:backIncpost,:)
            writetable(newMat, strcat('sub', num2str(k), 'postrating.csv'))
            frontIncpost = frontIncpost + 4 % this incrementing isn't ideal, maybe improve this using unique later
            backIncpost = backIncpost + 4
        end
    newMat(:,1) = [] 
    
    end

end