How to execute several mat file in a loop.
2 次查看(过去 30 天)
显示 更早的评论
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{:}];
0 个评论
采纳的回答
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
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{:}];
更多回答(1 个)
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
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
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 Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!