%this will run the IED plot through all the sessions of one animal and
%create IED as well as figures of the power and detected events
main = pwd;
allSess = dir('*_sess*');

for kk =6:size(allSess,1)
    fprintf(' ** Summary %3.i of %3.i... \n',kk, size(allSess,1));
    cd(strcat(allSess(kk).folder,'\',allSess(kk).name));
    IEDplot;
end

A = NaN(size(allSess,1),2);
for kk =1:size(allSess,1)
    fprintf(' ** Summary %3.i of %3.i... \n',kk, size(allSess,1));
    cd(strcat(allSess(kk).folder,'\',allSess(kk).name));
targetFile = dir('*.IED.mat'); load(targetFile(1).name); 
Epi_Events.duration=[];
Epi_Events.incidence = [];
Epi_Events.incidence = size(Epi_Events.peaks,1);
Epi_Events.duration= Epi_Events.timestamps(:,2)-Epi_Events.timestamps(:,1);
Epi_Events.mean_duration = [];
Epi_Events.mean_duration = mean(Epi_Events.duration);
filename = split(pwd,filesep); filename = filename{end};
save([filename '.IED.mat'],'Epi_Events');
A(kk,1) = Epi_Events.incidence;
A(kk,2) = Epi_Events.mean_duration;

end

figure, subplot (2,1,1)
bar(A(:,1))
hold on
title('IED incidence')

subplot (2,1,2)
bar(A(:,2))
hold on
title('IED duration')





