
%% detect folders
clear
Folders_1=dir(cd);
isub = [Folders_1(:).isdir]; %# returns logical vector
Folders_1(~isub)=[];
remove=[];
for i=1:length(Folders_1)
if Folders_1(i).name=="." | Folders_1(i).name==".."
remove=[remove i];
end
end
Folders_1(remove)=[];
clear isub remove i
%%

currentPath=cd;
for folderi=1: length(Folders_1)
folderi
disp(Folders_1(folderi).name)
cd (Folders_1(folderi).name)
if any(size(dir([ 'goodstims.mat' ]),1))
    Files=dir(['VariablesAt*.mat']);
    load([Files(end).name]);
    allVars(folderi).mean_T_rise=mean_T_rise;
    allVars(folderi).mean_T_rise_1=mean_T_rise_1;
    allVars(folderi).mean_T_rise_2=mean_T_rise_2;
    if contains(Folders_1(folderi).name,"min")
        allVars(folderi).power=Folders_1(folderi).name(13:19);
    else allVars(folderi).power=Folders_1(folderi).name(13:16);
    end
end
clearvars -except folderi allVars Folders_1 currentPath
cd(currentPath)
end
 
save(['allVars.mat'],'allVars','-v7.3')
clear


%% plot
load('allVars.mat')
power_dbm=["min9dbm", "min7dbm","min5dbm","min3dbm","min1dbm","1dbm","3dbm","5dbm"];
power_W=[5.57 9.29 15.07 24.25 37.52 52.81 62.71 67.72];

for ii=1:length(allVars)
   if ~isempty(allVars(ii))
      allVars(ii).power_W=power_W(find(power_dbm==string(allVars(ii).power)));   
      allVars(ii).T_rise_1sec=allVars(ii).mean_T_rise/50;
   end
end

[x,idx]=sort([allVars.power_W]);
allVars=allVars(idx);

fig=figure();hold on;
plot([allVars.power_W],[allVars.T_rise_1sec],'-x','linewidth',2)
xlabel('Radiate Power (W)')
ylabel('1sec \Delta T (C)')
set(gca,'fontweight','bold','fontsize',12,'linewidth',1)
set(fig,'Renderer', 'painters')

saveas(gca,fullfile(cd,['1sTriseVSpower.svg']))
saveas(gca,fullfile(cd,['1sTriseVSpower.pdf']))
saveas(gca,fullfile(cd,['1sTriseVSpower.fig']))

save(['allVars.mat'],'allVars','-v7.3')


%% plot Dleta_T for all powers
% detect folders
clear
Folders_1=dir(cd);
isub = [Folders_1(:).isdir]; %# returns logical vector
Folders_1(~isub)=[];
remove=[];
for i=1:length(Folders_1)
if Folders_1(i).name=="." | Folders_1(i).name==".."
remove=[remove i];
end
end
Folders_1(remove)=[];
clear isub remove i

fig=figure();hold on;
cmap = colormap(parula(15));
colors={cmap(5,:),cmap(3,:),cmap(1,:),cmap(7,:),cmap(9,:),cmap(11,:),cmap(13,:),cmap(15,:)}

for folderi=1: length(Folders_1)
folderi
cd (Folders_1(folderi).name)
if any(size(dir([ 'goodstims.mat' ]),1))
    Files=dir(['VariablesAt*.mat']);
    load([Files(end).name]);
    
    stdshade(StimTraces','sem',0.3,colors{folderi},(0:size(StimTraces',2)-1)/NewSampleRate-time_prestim,1);

    
end
clearvars -except folderi allVars Folders_1 currentPath colors cmap fig
cd('Y:\Analysed Data\Temp_for_RF\OM201\Multi_ContinousWave')
end

ylabel('Temperature (C)')
xlabel('time (s)')
title(['Temp response to RF'])
set(gca,'fontweight','bold','fontsize',11,'linewidth',1)
set(fig,'Renderer', 'painters')

%% save all variables
save(['VariablesAt' datestr(now,'mmddyyHHMMSS') '.mat'],'-v7.3')
