- 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...
load .mat files in loop
5 ビュー (過去 30 日間)
古いコメントを表示
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.
- 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
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:
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
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!
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
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)
6 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Cell Arrays についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!