%{

This code takes the preprocessed data and calculates the Grand Averaged ERPs for each subject for each event

%}
%%
typesOfEvents   = {'FF','UF','RO'};
noTypesOfEvents = length(typesOfEvents);

% Choose a type of event
idEvent          = 1;  % 'FF','UF',or 'RO'

% Choose a subject of interest %1, 2, 3...15
idSubject        = 1;

%{

You will have to manually put in each ID every time to get a different outcome

Example:
Subject 23 looking at a random object would be:
idEvent = 3
idSubject = 6

%}

eventInterest = typesOfEvents{idEvent};
targetDataFolder = 'C:\Users\Caroline\Desktop\LABFILES\Collier\Data\';
dataFiles = dir(strcat(targetDataFolder,'Subject*',eventInterest,'.mat'));
ftDataFileName  = dataFiles(idSubject).name; 

load 16elec_realigned.mat; % electrode configuration aligned to the MNI coordinate system
load 16layout.mat;         % electrode layout

%Just preliminary things here that we saw in the first code
%% Load a target data file
%%
load(ftDataFileName,'dataFT');

%Load the data preprocessed from the first code
%% Visualize the average ERPs
%%
noChannels = length(dataFT.label);
for ct = 1:noChannels
    % Averaging over the trials using ft_timelockanalysis
    cfg         = [];
    cfg.channel = {'all'};
    erpAvg      = ft_timelockanalysis(cfg,dataFT);
    
    % Manually pull out the trial ERPs
    noTrials = length(dataFT.trial);
    noSamples = length(dataFT.trial{1}(1,:));
    erps = nan(noTrials,noSamples);
    for ctT = 1:noTrials
        erps(ctT,:) = dataFT.trial{ctT}(ct,:);
    end    

%{
    
This section is pretty self-explanatory.
    
    
    
    


%}
    
    figure;
    FigureSet(2);
    hold on;        
        hp = patch([erpAvg.time';erpAvg.time(end:-1:1)';erpAvg.time(1)],[erpAvg.avg(ct,:)'-std(erps)';erpAvg.avg(ct,end:-1:1)'+std(erps(end:-1:1))';erpAvg.avg(ct,1)-std(erps(:,1))],[1 0.75 0.75]);
        plot(erpAvg.time,erpAvg.avg(ct,:),'r','LineWidth',5);
        plot(erpAvg.time,zeros(size(erpAvg.time)),'k:');
        title(eventInterest);
        ylim([-35 35]);
        ylabel('Amplitude (\muV)');
    hold off;
    box off;
    title(dataFT.label{ct});
    pause;
    close all;
end

%{

Here is where you can edit the plots you make as needed.

hold: This means to hold the image on the screen or not. You can change
this to on if you'd like to save each image, or off if you would just like
to run through all data.

pause: Comment this out if you'd like the loop to pause at every image
created. To unpause, all you have to do is press any key to continue the
loop.

%}

