How to execute several mat file in a loop.

I have 5 mat files in a folder.
Battery_Power_280.mat
Battery_Power_300.mat
Battery_Power_320.mat
Battery_Power_340.mat
Battery_Power_360.mat
whenever executing loop, it's stopped and showing no such file directory(Error: Unable to read file 'Battery_Power_1.mat'. No such file or directory).
But it should run untill N,if does not get "Battery_Power_1"(k=1)then it sould go for next step (k=2).
Would you please light up about this ? If (k=280:20:360) then it executes successfully. But i want the k value (1:1:N)
CODE:
N=500;
C = cell(1,N);
for k = 1:1:N
F = sprintf('Battery_Power_%d.mat',k);
S = load(F);
C{k} = S.Data_BatteryPower.signals.values(:,1);
end
M = [C{:}];

 採用された回答

Voss
Voss 2021 年 12 月 17 日

0 投票

To have k = 1 correspond to mat file 280, and k = 2 correspond to mat file 300, and so on, you can do this:
N=500;
mat_file_nums = 280:20:360;
C = cell(1,N);
for k = 1:1:N
F = sprintf('Battery_Power_%d.mat',mat_file_nums(k));
S = load(F);
C{k} = S.Data_BatteryPower.signals.values(:,1);
end
M = [C{:}];

7 件のコメント

Arif Hoq
Arif Hoq 2021 年 12 月 17 日
Error:
Index exceeds the number of array elements (5).
Error in testing2 (line 5)
F = sprintf('Battery_Power_%d.mat',mat_file_nums(k));
In my folder it has 5 mat files. whenever k=6, then it shows this error. what can be the reason?
Voss
Voss 2021 年 12 月 17 日
I guess N should be 5 then. Why is it 500?
Arif Hoq
Arif Hoq 2021 年 12 月 17 日
actually i am thinking about more mat files,like 50 files. it can be more or less. so N should be a approximate value.
Your code is correct with definite files and filename.
But if i want to load all mat files(suppose, Battery_Power_10.mat,Battery_Power_13.mat,Battery_Power_18.mat,Battery_Power_25.mat) then showing error. because of index value k.
if I change this (mat_file_nums = 1:1:N;) then error happens.
Voss
Voss 2021 年 12 月 17 日
編集済み: Voss 2021 年 12 月 17 日
If you try to load a mat file that does not exist, an error will occur and the code will stop execution. If you want the loop to continue instead, you can handle the error with try/catch or you can check that the mat file exists before you try to load it.
Arif Hoq
Arif Hoq 2021 年 12 月 17 日
could you please share the "continue" systax or try/catch systax?
thank you.
Voss
Voss 2021 年 12 月 17 日
To check for the files' existence:
N=50;
mat_file_nums = 115:5:360; % 50 numbers
C = cell(1,N);
for k = 1:1:N
F = sprintf('Battery_Power_%d.mat',mat_file_nums(k));
if exist(F,'file')
S = load(F);
C{k} = S.Data_BatteryPower.signals.values(:,1);
end
end
M = [C{:}];
To try/catch:
N=50;
mat_file_nums = 115:5:360;
C = cell(1,N);
for k = 1:1:N
F = sprintf('Battery_Power_%d.mat',mat_file_nums(k));
try
S = load(F);
catch
continue
end
C{k} = S.Data_BatteryPower.signals.values(:,1);
end
M = [C{:}];
Arif Hoq
Arif Hoq 2021 年 12 月 20 日
got my expected solution. Thanks @Benjamin

サインインしてコメントする。

その他の回答 (1 件)

Image Analyst
Image Analyst 2021 年 12 月 17 日

0 投票

Try this:
folder = pwd;
filePattern = 'Battery*.mat';
fileList = dir(filePattern);
for k = 1 : length(fileList)
[~, thisFileName, ext] = fileparts(fileList(k).name);
% Optional user prompt to load the mat file, skip it, or quit.
promptMessage = sprintf('Do you want to load file %d of %d:\n%s,\nor Quit processing?', ...
k, length(fileList), thisFileName);
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Load', 'Quit', 'Skip', 'Load');
if contains(buttonText, 'Load', 'IgnoreCase', true)
fprintf('\nLoading "%s".m.\n', thisFileName)
S = load(thisFileName)
elseif contains(buttonText, 'Quit', 'IgnoreCase', true)
break;
end
end

3 件のコメント

Arif Hoq
Arif Hoq 2021 年 12 月 17 日
its working only for the last mat file. should i use cell array for all mat files ?
Walter Roberson
Walter Roberson 2021 年 12 月 17 日
folder = pwd;
filePattern = 'Battery*.mat';
fileList = dir(filePattern);
saved_data = {};
for k = 1 : length(fileList)
[~, thisFileName, ext] = fileparts(fileList(k).name);
% Optional user prompt to load the mat file, skip it, or quit.
promptMessage = sprintf('Do you want to load file %d of %d:\n%s,\nor Quit processing?', ...
k, length(fileList), thisFileName);
titleBarCaption = 'Continue?';
buttonText = questdlg(promptMessage, titleBarCaption, 'Load', 'Quit', 'Skip', 'Load');
if contains(buttonText, 'Load', 'IgnoreCase', true)
fprintf('\nLoading "%s".m.\n', thisFileName)
S = load(thisFileName);
saved_data{end+1} = S;
elseif contains(buttonText, 'Quit', 'IgnoreCase', true)
break;
end
end
Note that each entry in saved_data will be a struct with one field for each variable in the file.
Image Analyst
Image Analyst 2021 年 12 月 17 日
@Mohammad Ariful Hoq it works for all mat files, not just the last one. What you should do after the data is read into S is to call some function that processes S and returns results.
If you want, you can save all the data into a cell array like Walter showed above (unhide comments). But then you'll just have to have a second for loop to process all the data you've saved in the saved_data cell array. I think it's better to just do it all in one for loop. Is there some other reason why you'd need all the saved data after the loop exits???

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeScope Variables and Generate Names についてさらに検索

質問済み:

2021 年 12 月 16 日

コメント済み:

2021 年 12 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by