I want to load multiple files in few folders and to plot figures for each. I'm quite naive to matlab environment, I did something dirty like this!:
foldername1='K_S/';
foldername2='L_J/';
foldername3='P_O/';
extension='*.pro';
name1=strcat(foldername1, extension);
name2=strcat(foldername2, extension);
name3=strcat(foldername3, extension);
fileset1=dir(name1);
fileset2=dir(name2);
fileset3=dir(name3);
a = [];
b = [];
c = [];
for i = 1:length(fileset1)
a=load(strcat(foldername1, fileset1(i).name));
b=load(strcat(foldername2, fileset2(i).name));
c=load(strcat(foldername3, fileset3(i).name));
figure
sub1=plot(a(:,1),a(:,2),'b'), hold on
sub2=plot(a(:,1),a(:,3),'r')
ylabel('score')
xlabel('trial')
legend([sub1, sub2],'sub1','sub2')
legend boxoff
title(sprintf('Dyad K S: %d',i));
axis square
figure
sub1=plot(b(:,1),b(:,2),'b'), hold on
sub2=plot(b(:,1),b(:,3),'r')
ylabel('score')
xlabel('trial')
legend([sub1, sub2],'sub1','sub2')
legend boxoff
title(sprintf('Dyad L J: %d',i));
axis square
figure
sub1=plot(c(:,1),c(:,2),'b'), hold on
sub2=plot(c(:,1),c(:,3),'r')
ylabel('score')
xlabel('trial')
legend([sub1, sub2],'sub1','sub2')
legend boxoff
title(sprintf('Dyad P O: %d',i));
axis square
end
Any help would be appreciated on how to make it smarter.
Thank you!
You should use cells in matlab, which allows to store multiple obejcts into a single variable. Something like that
foldername ={'K_S/', 'L_J/', 'P_O/'};
extension='*.pro';
for k = 1:length(foldername)
name=strcat(foldername{k}, extension);
fileset=dir(name);
a = [];
for i = 1:length(fileset)
a=load(strcat(foldername{k}, fileset(i).name));
figure
sub1=plot(a(:,1),a(:,2),'b'), hold on
sub2=plot(a(:,1),a(:,3),'r')
ylabel('score')
xlabel('trial')
legend([sub1, sub2],'sub1','sub2')
legend boxoff
title(sprintf(['Dyad' strrep(foldername{k}, '_', ' ') ': %d'],i));
axis square
end
end