How to execute several mat file in a loop.

2 vues (au cours des 30 derniers jours)
Arif Hoq
Arif Hoq le 16 Déc 2021
Commenté : Arif Hoq le 20 Déc 2021
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{:}];

Réponse acceptée

Voss
Voss le 17 Déc 2021
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 commentaires
Voss
Voss le 17 Déc 2021
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 le 20 Déc 2021
got my expected solution. Thanks @Benjamin

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 17 Déc 2021
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 commentaires
Walter Roberson
Walter Roberson le 17 Déc 2021
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 le 17 Déc 2021
@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???

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by