How to concatenate multiple .mat files and generate a 3D matrix?

1 回表示 (過去 30 日間)
Andres Ulloa
Andres Ulloa 2022 年 8 月 3 日
コメント済み: Andres Ulloa 2022 年 8 月 3 日
I have 100 files which are 30482x43 (number of data x layers), and what I am trying to do is make a single 30482x100x43 matrix.
I would appreciate your help please!!

採用された回答

Stephen23
Stephen23 2022 年 8 月 3 日
編集済み: Stephen23 2022 年 8 月 3 日
"I have 100 files ..."
which are probably named using some sequential numbering, so you will need to take that into account in the code:
P = 'absolute or relative path to where the files are saved';
S = dir(fullfile(P,'*.mat'));
S = natsortfiles(S); % download: https://www.mathworks.com/matlabcentral/fileexchange/47434-natural-order-filename-sort
for k = 1:numel(S)
F = fullfile(P,S(k).name);
L = load(F);
S(k).data = L.nameOfTheVariableWhichYouDidNotTellUs;
end
A = permute(cat(3,S.data),[1,3,2]);
  1 件のコメント
Andres Ulloa
Andres Ulloa 2022 年 8 月 3 日
thank you so much you helped me a lot!!

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

その他の回答 (1 件)

KSSV
KSSV 2022 年 8 月 3 日
matFiles = dir('*.mat') ;
N = length(matFiles) ;
iwant = zeros(3048,43,N) ;
for i = 1:N
S = matfile(matFiles(i).name) ;
iwant(:,:,i) = S.myvar ; % myvar is the variable in your mat file
end

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品


リリース

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by