load .mat files in loop

4 vues (au cours des 30 derniers jours)
akriti
akriti le 11 Fév 2024
Commenté : Stephen23 le 12 Fév 2024
I want to load .mat files saved in a specific foldar and perform some operations on each one of them and then save the answer in an array.
  1. Load .mat files in sequence with file names
S_out_02-10-2024 16-55.mat; S_out_02-10-2024 16-56.mat;S_out_02-10-2024 16-57.mat;S_out_02-10-2024 16-58.mat;........
content of each file is 3x6 double matrix
2. Do some calculations on data received from each file.
3. Save the output from calculation into another .mat file with 1 column and rows as output of each calculations.
Please suggest an effcient matlab code for the same.
  1 commentaire
Stephen23
Stephen23 le 11 Fév 2024
Modifié(e) : Stephen23 le 11 Fév 2024
"Please suggest an effcient matlab code for the same."
I presume that those digits represent dates and that by "in sequence" you actually mean in chronological order. If the dates had been given in an ISO 8601 date format (i.e. largest to smallest units) then you could trivially SORT them into chronological order.
However, because the date units are all reversed (i.e. smallest to largest) you make this task much more difficult. You will have to do something like:
  • use DIR and parse the filenames into some kind of date representation (e.g. DATETIME) and SORT that.
  • build the filenames from lists of all known units that exist in the filenames.
  • something else...
Either way it will be more effort compared to if the filenames had been more carefully defined. Better data design makes code simpler, more robust, and much more efficient.

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 11 Fév 2024
Modifié(e) : Stephen23 le 11 Fév 2024
Create some fake data files (it is much better when you provide some data files for us to work with):
X=1; save 'S_out_02-10-2024 16-55.mat' X
X=2; save 'S_out_02-10-2024 16-56.mat' X
X=3; save 'S_out_02-10-2024 16-57.mat' X
X=4; save 'S_out_02-10-2024 16-58.mat' X
X=5; save 'S_out_01-11-2024 16-58.mat' X
Get filenames:
P = '.'; % absolute or relative path to where those files are saved
S = dir(fullfile(P,'S_out*.mat'));
{S.name} % note: NOT in chronological order!
ans = 1×5 cell array
{'S_out_01-11-2024 16-58.mat'} {'S_out_02-10-2024 16-55.mat'} {'S_out_02-10-2024 16-56.mat'} {'S_out_02-10-2024 16-57.mat'} {'S_out_02-10-2024 16-58.mat'}
Sort into chronological order:
D = datetime({S.name},"InputFormat","'S_out'_d-M-y H-m'.mat'");
[~,X] = sort(D);
S = S(X);
{S.name} % now in chronological order
ans = 1×5 cell array
{'S_out_02-10-2024 16-55.mat'} {'S_out_02-10-2024 16-56.mat'} {'S_out_02-10-2024 16-57.mat'} {'S_out_02-10-2024 16-58.mat'} {'S_out_01-11-2024 16-58.mat'}
Process & save:
N = numel(S);
Z = nan(N,1);
for k = 1:N
F = fullfile(S(k).folder,S(k).name);
A = load(F);
M = A.X; % change "X" to whatever name your matrix has
Z(k) = sqrt(M); % do your calculations on matrix M
end
save('output.mat','Z')
The output:
disp(Z)
1.0000 1.4142 1.7321 2.0000 2.2361
  6 commentaires
akriti
akriti le 11 Fév 2024
Thank you very much for your help. I was able to solve the problem.
Stephen23
Stephen23 le 12 Fév 2024
@akriti: Please remember to click the accept button if it helped!

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by