Batch processing using for loop

조회 수: 7 (최근 30일)
Karoline Opsahl
Karoline Opsahl 2022년 5월 21일
편집: Voss 2022년 5월 22일
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
Voss
Voss 2022년 5월 21일
"it didn't work"
What did it do (or not do) that was different than what you expected?
Karoline Opsahl
Karoline Opsahl 2022년 5월 21일
only the variables form the first file in the folder was uploaded to workspace

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2022년 5월 21일
편집: Voss 2022년 5월 21일
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
Karoline Opsahl
Karoline Opsahl 2022년 5월 21일
thank you for helping!
Voss
Voss 2022년 5월 21일
편집: Voss 2022년 5월 22일
You're welcome!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2022년 5월 21일
@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:

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by