load files within a for loop

21 views (last 30 days)
Beatriz Sanchez
Beatriz Sanchez on 28 Jan 2019
Commented: Image Analyst on 28 Jan 2019
Hi!
so, I run my code several times with differents values for some of the parameters, and I saved those results on a .mat files on a folder. What I want to do is to load that files within a for loop so I can plot them one by one. First I create a list of those files I want to plot in the workspace called "bea" and then I tried to load them in the for loop, but is not posible because matlab dosn't recognize "bea" as a file name:
cd 'C:\Users\David\Documents\Tesis\matlab\resultados\saves' %folder where are the files I want to load
folder= dir ('*.mat')
bea={folder.name} %this give a list of the files names I want to load
for id=1:16
load (bea.mat(id)) % in here, I tried to load those files one by one
for ix= 1:rep
hold on
scatter (vec, x_mean(ix, :, 1),'b','filled')
scatter (vec, x_mean(ix, :, 2),'r', 'filled')
end
plot (vec, rep_prom1,'b')
plot (vec, rep_prom2, 'r')
set (gca, 'Xtick', vec, 'Xticklabel', vec)
[ax1,h1]=suplabel('Delta m2');
[ax2,h2]=suplabel('Densidad Poblacional','y');
[ax3,h3]=suplabel(strcat('Densidad poblacional. ', ' Alpha=' ,num2str(alpha), '. p=', num2str(p), '. Respuesta Sigmoide (s=' , num2str(2), ')') ,'t');
set(h3,'FontSize',18)
set(h1,'FontSize',14)
set(h2,'FontSize',14)
print('Densidades Promedio', '-dpng')
end
Any Idea About how to do it? is it possible to load files in a for loop? thanks

Accepted Answer

Geoff Hayes
Geoff Hayes on 28 Jan 2019
Beatriz - isn't folder a struct array with all files that are of extension *.mat? If that is the case, can't you just iterate over each struct in the array and load that file? Perhaps something like
cd 'C:\Users\David\Documents\Tesis\matlab\resultados\saves' %folder where are the files I want to load
myFiles = dir ('*.mat');
for k=1:length(myFiles)
myData = load(myFiles(k).name);
% plot your data
end
  2 Comments
Beatriz Sanchez
Beatriz Sanchez on 28 Jan 2019
thank you for your answer!
I did what you told me to, but now when I run the code, it dosn't give me any plot, it says "undefined function or variable ...." and those "undefined" variables are the ones that supposed to be on the files that I'm loading.
Image Analyst
Image Analyst on 28 Jan 2019
You need to extract them from the myData structure, like
rep_prom1 = myData.rep_prom1;
x_mean = myData.x_mean;
Attach one of the .mat files if you still have problems.

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products


Release

R2016a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!