load several (one after another ) .mat Files via a For-Loop Job

Hello, i have a Program that is in the Folder-Directory: Main. And i have some several numbered .mat-Files with the Variable Buff that are in the Folder Directory Main/Combination:
combinations_1.mat
combinations_2.mat
combinations_3.mat
...and so on
My Program should load the first combinations_1.mat and to something:
for I = 1:size(Buff,1)
% calculate some stuff
end
After the Loop is done it should load combinations_2 and start the Loop again
My Idea is:
d = dir('*.mat'); % only looking for .mat-Files
Number_mat = length(d); % number of .mat-Files
for i=1:Number
load(['combinations_' num2str(i) '.mat'])
end
But i no idea how i can this combine with the For-Loop. Hope everybody understand my aim, big thanks

 Réponse acceptée

Sarah Wait Zaranek
Sarah Wait Zaranek le 17 Jan 2013
Modifié(e) : Jan le 17 Jan 2013
Unless I am missing something, I think the easiest thing would be to do a nested for loop.
for ii = 1: Number
load(['combinations_' num2str(ii) '.mat'])
for jj = 1:size(Buff,1)
% calculate some stuff
end
end

4 commentaires

Hi Sarah, I have the same problem. Could you please explain what the second for does? I can't understand it.
Hi Sarah,
I also have a similar question. I am trying to subplot the same variable from each of N mat files in a folder. I tried a nested for loop and an if condition. Right now it puts the same variable in all rows (the first one it loads). But I think it might work if I could load each mat file sequentially and refer to the same subplot in the loop. Please could you think of some ways I could adapt my code to do this? I would greatly appreciate your tips! My current code is as follows:
%%
read_folder = '...';
write_folder = '...';
cd(read_folder);
files = dir('*.mat');
N=numel(files);
R_y = [array of numbers]';
A_y = [another array of numbers]';
figure;
fig = gcf;
for i = 1:N
for j = 1:6 %will need to change this as per numel(files).
load(files(i).name, 'R', 'A')
[pathstr,name,ext] = fileparts(files(i).name);
newFilename = fullfile(pathstr,name);
subplot(N,2,j)
if mod(j,2) == 0
subplot(N,2,j);
plot(A_y, A);
else mod(j,2) == 1 ;
subplot(N,2,j);
plot(R_y, R);
end
end
cd(write_folder);
saveas(fig, Image_name, 'png');
cd(read_folder);
end
Thanks!
I'd do
rows = ceil(sqrt(N*6));
outside the loop, then inside the loop do
subplot(rows, rows, (i-1)*6+j);
This will make a square matrix of plots and each plot will go into a separate, new plot in that matrix.
Thanks for your suggestion. I will test it soon.
Someone from MATLAB, Pujitha Narra, posted an answer on the question I asked, their solution worked really well!

Connectez-vous pour commenter.

Plus de réponses (1)

You can use dir() to get a list of the filenames. Then you should check that the variable is actually in the mat file. Try this. It's a fairly robust adaptation of code already in the FAQ.
myFolder = 'Main/combinations'; % May need to correct this.
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, 'combinations*.mat');
matFiles = dir(filePattern);
for k = 1:length(matFiles)
matFilename = fullfile(myFolder, matFiles(k).name)
matData = load(matFilename); % Retrieves a structure.
% See if Buff actually exists in the data structure.
hasField = isfield(matData, 'Buff');
if ~hasField
% Alert user in popup window.
warningMessage = sprintf('Buff is not in %s\n', matFilename);
uiwait(warndlg(warningMessage));
% It's not there, so skip the rest of the loop.
continue; % Go to the next iteration.
end
% If you get to here, Buff existed in the file.
Buff = matData.Buff; % Extract the Buff from the structure.
for row = 1 : size(Buff,1)
% Calculate some stuff
end
end

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by