필터 지우기
필터 지우기

load .mat files in loop

조회 수: 2 (최근 30일)
akriti
akriti 2024년 2월 11일
댓글: Stephen23 2024년 2월 12일
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
Stephen23
Stephen23 2024년 2월 11일
편집: Stephen23 2024년 2월 11일
"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.

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

채택된 답변

Stephen23
Stephen23 2024년 2월 11일
편집: Stephen23 2024년 2월 11일
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
akriti
akriti 2024년 2월 11일
Thank you very much for your help. I was able to solve the problem.
Stephen23
Stephen23 2024년 2월 12일
@akriti: Please remember to click the accept button if it helped!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by