Batch processing using for loop

7 vues (au cours des 30 derniers jours)
Karoline Opsahl
Karoline Opsahl le 21 Mai 2022
Modifié(e) : Voss le 22 Mai 2022
I want to load multiple files using a for loop. I tried this code:
EXAM_BEV3201_V20=uigetdir;
cd(EXAM_BEV3201_V20);
files=dir('acc_data*.mat');
for i=1:length(files)
load(files(i) .name);
end
but it didn't work.
Can someone please try to figure out what I have done wrong?
  2 commentaires
Voss
Voss le 21 Mai 2022
"it didn't work"
What did it do (or not do) that was different than what you expected?
Karoline Opsahl
Karoline Opsahl le 21 Mai 2022
only the variables form the first file in the folder was uploaded to workspace

Connectez-vous pour commenter.

Réponse acceptée

Voss
Voss le 21 Mai 2022
Modifié(e) : Voss le 21 Mai 2022
load with no output loads the contents of the mat file into the workspace, overwriting any variables of the same name that were already in the workspace. Therefore, if any mat file contains variables of the same name as variables in another mat file, then at the end of the loop, the values of those variables will come from the last mat file to be loaded.
To avoid this problem, you can load each mat file into a separate element of a cell array (or if all the mat files contain the same set of variable names, you can load each one into a separate element of a struct array).
Here's loading each file into an element of a cell array:
% prompt the user to select a directory:
EXAM_BEV3201_V20 = uigetdir();
% if a directory was selected ...
if ~isequal(EXAM_BEV3201_V20,0)
% get info about all acc_data*.mat files in that
% directory (no need to use cd):
files = dir(fullfile(EXAM_BEV3201_V20,'acc_data*.mat'));
% construct the full path names of those files:
files = fullfile(EXAM_BEV3201_V20,{files.name});
% load each file, store everything in the cell array C:
C = cell(1,numel(files));
for i = 1:numel(files)
C{i} = load(files{i});
end
end
Then you can access the contents of each mat file by doing C{1}, C{2}, etc.
  2 commentaires
Karoline Opsahl
Karoline Opsahl le 21 Mai 2022
thank you for helping!
Voss
Voss le 21 Mai 2022
Modifié(e) : Voss le 22 Mai 2022
You're welcome!

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 21 Mai 2022
@Karoline Opsahl, you say "only the variables form the first file in the folder was uploaded to workspace" -- that's very strange. I would think that only the last iteration would survive since the iterations overwrite all the prior variables (if the variables have the same names).
Don't use cd and don't use i (the imaginary variable) for a loop counter! Build the full filename instead.
folder = uigetdir;
files=dir(fullfile(folder, 'acc_data*.mat'));
numFiles = numel(files);
for k = 1 : numFiles
thisFileName = fullfile(files(k).folder, files(k).name);
fprintf('Loading file #%d of %d : "%s"\n', k, numFiles, thisFileName);
allMatFiles{k} = load(thisFileName);
end
fprintf('Done loading %d mat files from folder "%s".\n', k, numFiles, folder);
See the FAQ on cd:

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by