How to execute several mat file in a loop.

조회 수: 2 (최근 30일)
Arif Hoq
Arif Hoq 2021년 12월 16일
댓글: Arif Hoq 2021년 12월 20일
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일
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
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일
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
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???

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by