Merging contents of multiple MAT files

2 ビュー (過去 30 日間)
Multiplexer
Multiplexer 2018 年 10 月 25 日
編集済み: Stephen23 2022 年 1 月 20 日
Hello,
I got a collection of MAT files (generated by External Mode data archiving) that have names such as below and their count can go into 1000's: - MyMatFile_0.mat - MyMatFile_1.mat (...) - MyMatFile_2999.mat - MyMatFile_3000.mat
What i want to achieve is to merge all the arrays from inside the MAT files vertically together (like [MyMatFile_0.mat ; MyMatFile_1.mat ... ] and save that new single array to a spearate MAT file or simply leave it in workspace.
So for, example, with the files I'm attaching, each of them contains a 20x2 double array and I want to merge them together to make one MAT file with 60x2 double array.
Also, this batch of files is generated per each experiment i do and for each experiment the total number of MAT files may vary and the amount of elements in array in each MAT file also may vary (but there will be always 2 columns).
Any help is appreciated.

採用された回答

Stephen23
Stephen23 2018 年 10 月 25 日
編集済み: Stephen23 2021 年 4 月 18 日
To read the files in the correct order download my FEX submission natsortfiles:
Then all you need is this (the input and output files are attached):
D = 'path to the folder where the files are saved'
D = '.';
S = dir(fullfile(D,'TestLog_*.mat'));
S = natsortfiles(S); % alphanumeric sort by filename
for k = 1:numel(S)
T = load(fullfile(D,S(k).name));
S(k).data = struct2cell(T);
end
M = vertcat(S.data);
save('TestLog.mat','M')
This will work for any number of .mat files, with a few conditions:
  • each .mat file contains only one variable.
  • the output file is in a different folder or does not match the dir filename string (otherwise you could end up accumulating the same data repeatedly).
  9 件のコメント
Hanah Aemilia Choice
Hanah Aemilia Choice 2022 年 1 月 20 日
編集済み: Hanah Aemilia Choice 2022 年 1 月 20 日
When I run this script (the first posted solution), I get the error message:
Reference to non-existent field 'data'.
Error in displayscript (line 23)
M = vertcat(S.data);
I'm wondering what the cause of this would be.
Stephen23
Stephen23 2022 年 1 月 20 日
編集済み: Stephen23 2022 年 1 月 20 日
"I'm wondering what the cause of this would be."
No files match the DIR name pattern so S is sempty, so the loop never iterates, so the data field is not created.
Take a look at numel(S)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by