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.
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 ?
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 Exchange에서 Scope 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