How can i load .mat files from subfolders with for loop?

I have a different number of .mat files in several folders, which I would like to import, plot data und save it in the same folder as a pdf file.
Here an example with two folders:
C:\Users\I2d1_4_1_1\
y3.mat
y4.mat
y5.mat
C:\Users\I5d1_4_1_1\
y3.mat
y4.mat
y5.mat
y6.mat
My two questions are:
  1. How do i save it in the same folder without changing the directory.
  2. How to adapt the code that i dont need to change the for loop i=3:5 for the first folder and i=3:6 for the second folder.
DataPath ='C:\Users\';
for i=3:60
for input=[2 5 8]
for delay=[4 8 12];
for delayinput=[1 4 8];
load([DataPath 'I%dd1_%d_1_%d\y' num2str(i) '.mat',input,delay,delayinput])
figure(1), plotregression(x, y);
SaveName = sprintf('NN_%d_1_%d',i,delay);
saveas(figure(1),fullfile(DataPath 'I%dd1_%d_1_%d', SaveName),'pdf')
end
end
end
end
Thanks for your help.

 Accepted Answer

projectdir = 'C:\Users';
foldinfo = dir( fullfile(projectdir, 'I*') );
foldinfo(~[foldinfo.isdir]) = []; %get rid of I* that are not folders
foldnames = fullfile(projectdir, {foldinfo.name});
numfold = length(foldnames);
for didx = 1 : numfold
thisfold = foldnames{didx};
dinfo = dir(fullfile(thisfold, 'y*.mat'));
filenames = fullfile(thisfold, {dinfo.name});
numfiles = length(filenames);
for fidx = 1 : numfiles
thisfile = filenames{fidx};
datastruct = load(thisfile);
x = datastruct.x; y = datastruct.y;
and do your thing
end
end

5 Comments

Thanks, thats exactly what I was looking for.
I would now have two more questions.
How can I write the maximum of the data points x and y (sqrt (x2 + y2)) from each folder into a structure and read them into an Excel spreadsheet?
And the second question is how do I get all the values ​​in a plot (x, y) in which the folders are distinguished in the legend?
projectdir = 'C:\Users';
foldinfo = dir( fullfile(projectdir, 'I*') );
foldinfo(~[foldinfo.isdir]) = []; %get rid of I* that are not folders
foldnames = fullfile(projectdir, {foldinfo.name});
numfold = length(foldnames);
all_min_x = zeros(1,numfold);
all_min_y = zeros(1,numfold);
for didx = 1 : numfold
thisfold = foldnames{didx};
[~, fold_name_only] = basename(thisfold);
dinfo = dir(fullfile(thisfold, 'y*.mat'));
filenames = fullfile(thisfold, {dinfo.name});
numfiles = length(filenames);
fold_min_x = inf; fold_min_y = inf;
for fidx = 1 : numfiles
thisfile = filenames{fidx};
datastruct = load(thisfile);
x = datastruct.x; y = datastruct.y;
and do your thing
fold_min_x = min(fold_min_x, x);
fold_min_y = min(fold_min_y, y);
plot(x, y, 'DisplayName', fold_name_only);
hold on
drawnow ratelimit
end
all_min_x(didx) = fold_min_x;
all_min_y(didx) = fold_min_y;
end
now write all_min_x and all_min_y to your excel spreadsheet
Thank you. But the following line is producing an error.
[~, fold_name_only] = basename(thisfold);
Undefined function 'basename' for input arguments of type 'char'.
I am using Matlab 2014a. Maybe because of that?
[~, fold_name_only] = fileparts(thisfold);
It was a mistake on my part. Too many years of working with too many languages.
Now that is working but the line drawnow ratelimit gives an error as unknown command option.
I am really thankfull for your work.

Sign in to comment.

More Answers (1)

How about using dir function?
Using the function, the solution would be like this:
fileList = dir('C:\Users\**\*.mat');
for kk = 1:numel(fileList)
filePath = fullfile(fileList(kk).folder,fileList(kk).name);
load(filePath);
h = figure;
plotregression(x,y);
saveName = replace(fileList(kk).name,'.mat','.pdf');
saveas(h,fullfile(fileList(kk).folder,saveName));
close(h);
end

6 Comments

Thanks for that answer. I think that would work if i would not have other files in the folders too such as other .mat files and .pdf files.
Sry for my inaccurate example. I wanted to keep it as simple as possible.
OK, then you can skip unwanted .mat files by simply adding if statement.
For example, if you want to save in .pdf for only y1~y6.mat, the program would be like this:
fileList = dir('C:\Users\**\*.mat');
for kk = 1:numel(fileList)
if ~isempty(regexp(fileList(kk).name,'y[1-6].mat'))
filePath = fullfile(fileList(kk).folder,fileList(kk).name);
load(filePath);
h = figure;
plotregression(x,y);
saveName = replace(fileList(kk).name,'.mat','.pdf');
saveas(h,fullfile(fileList(kk).folder,saveName));
close(h);
end
end
Or, for any y*.mat but not other files beginning with a different letter:
fileList = dir('C:\Users\**\y*.mat');
And then skip the "if" inside the "for".
You might also want to take a look at fileDatastore().
Thanks for your help. I am usind Matlab 2014a and there the dir funktion this way is not working I think. Sry for the inaccurate question again.
Then, you should change the first line to:
fileList = dir('C:\Users\I2d1_4_1_1\*.mat');
and change the fileList(kk).folder to 'C:\Users\I2d1_4_1_1\'.
Then, please do the same thing to the other folder C:\Users\I5d1_4_1_1\.
OK, thanks. Thats what i more or less already do, but if there is no "automatical" way to do it I have to go on like this. But thanks for the help and your ideas.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!