Batch processing using for loop

10 ビュー (過去 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 Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by