現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
How do I combine 2d matrices mat files into a single 3d matrices file?
59 ビュー (過去 30 日間)
古いコメントを表示
Fadilla Atyka Nor Rashid
2018 年 8 月 13 日
I have a number of 240 x 320 double mat files. I wanted to combine around 30 of them to make it a single 3d mat file resulting to 240x320x30 double.
I have tried below codes, but something went wrong because all the values inside the files turns out to be zero and thats disrupt my output. Can someone helps me with this? Thank you in advance!
d=(240,320,30);
for k=1:30
d(:,:,k);
end
4 件のコメント
Stephen23
2018 年 8 月 13 日
@Fadilla Atyka Nor Rashid: your question does not contain enough information. How many variables are saved in each .mat file? Do they have the same name/s in each .mat file?
Fadilla Atyka Nor Rashid
2018 年 8 月 13 日
@kssv i have more than that but i wanna make it 3d matrices resulting to 240x320x30 (30 is the Number of frames)
@Dennis, thats why it looks like something wrong with my code. hehe
@Stephen each .mat file consists of 240x320uint16 yeah, they do have same name in each .mat file.
採用された回答
Stephen23
2018 年 8 月 13 日
編集済み: Stephen23
2018 年 8 月 13 日
Something like this should get you started. This will load the data from all of the .mat files in the specified directory, concatenate their data, and then save the concatenated array as a new .mat file:
D = 'path to where the files are';
S = dir(fullfile(D,'*.mat'));
N = numel(S);
C = cell(1,N);
for k = 1:N
T = load(fullfile(D,S(k).name));
C{k} = T.fieldname; % pick the fieldname to suit.
end
A = cat(3,C{:});
save('array.mat','A')
See also:
30 件のコメント
college student
2021 年 7 月 9 日
編集済み: college student
2021 年 7 月 9 日
I get the new matrix, but the values inside its matrix are like this. I can't plot it using imagesc or surf, could you help me?![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/678808/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/678808/image.jpeg)
Stephen23
2021 年 7 月 9 日
@college student: I cannot do anything with a screenshot. If you want help, upload your data in a .mat file.
college student
2021 年 7 月 9 日
編集済み: college student
2021 年 7 月 9 日
list of matrix I've tried to combine. the filedname is data_matrix
Neda Deljavan
2022 年 1 月 4 日
*******SOS********
@Stephen![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/852675/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/852675/image.png)
I wanna make a 3D matrix (30800,4,12)
each of 12 pages which is number of my subjects are not continuous to load them with a for loop. The name of them are specific.
I've used your code that was above, I cannot load all the .mat files.
Could you please help me in the same issue?
Stephen23
2022 年 1 月 4 日
@Neda Deljavan: are the .MAT file well-designed (with exactly the same variable names) or badly-designed (with different variable names) ?
Neda Deljavan
2022 年 1 月 5 日
@Stephen bad design! due to the condition of test, just some of them will be used which have different names.
First, I should load them one by one
Then, merge in A 3D matrix
but how(?) I do not know
Neda Deljavan
2022 年 1 月 5 日
I load each of files in workspace and then use this code
D = {P04EC1,P06EC1,P07EC1,P12EC1,P13EC1,P14EC1,P15EC1,P16EC1,P20EC1,P22EC1,P23EC1,P26EC1};
A = cat(3,D{:})
''cat'' function does not work! cuz the dimensions of each matrix are not same!
Neda Deljavan
2022 年 1 月 5 日
I load each of files in workspace and then use this code
D = {matrix1,matrix2,,,,};
A = cat(3,D{:})
''cat'' function does not work! cuz the dimensions of each matrix are not same!
Neda Deljavan
2022 年 1 月 5 日
I use this code
D = 'path to where the files are';
S = dir(fullfile(D,'*.mat'));
N = numel(S);
C = cell(1,N);
for k = 1:N
T = load(fullfile(D,S(k).name));
C{k} = T.fieldname; % pick the fieldname to suit.
end
A = cat(3,C{:});
save('array.mat','A')
the problem is that ''T'' and ''C{k}'' are not correct and I give some errors
T = load(fullfile(D,S(k).name));
C{k} = T.fieldname; % pick the fieldname to suit.
what should I put it in ''name'' and ''fieldname''
Stephen23
2022 年 1 月 5 日
編集済み: Stephen23
2022 年 1 月 5 日
"what should I put it in ''name'' and ''fieldname''
Because S is the structure returned by DIR "name" is already correct, you do not need to change it (if you change it, the code will not work).
For "fieldname" you need to use the name of the variable that you want to import (it is imported as a field of the structure S), just as I wrote in my original answer.
Neda Deljavan
2022 年 1 月 5 日
編集済み: Neda Deljavan
2022 年 1 月 5 日
I did not get the namefield
I tried the name of file but it did not work
I have 12 .mat files with different names
how can I put them together
the A matrix which is 3D did not make
Stephen23
2022 年 1 月 5 日
If each file contains exactly one variable then you can try the following:
D = 'path to where the files are';
S = dir(fullfile(D,'*.mat'));
for k = 1:numel(S)
F = fullfile(D,S(k).name);
C = struct2cell(load(F));
S(k).data = C{1};
end
A = cat(3,S.data);
save('array.mat','A')
Neda Deljavan
2022 年 1 月 5 日
May I share .mat files with you?
I think it will make sense
I really should say thank you
Am struggling more than 72h with this issue!
Due to limitatio of here I cannot share it even!
really get crazyy :((((((
Stephen23
2022 年 1 月 5 日
"''cat'' function does not work! cuz the dimensions of each matrix are not same!"
Yes, we can clearly see that the dimensions of your arrays are different:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/854200/image.png)
So you need to answer this question: how do you want to concatenate together arrays of different sizes?
For example, you might want to do one of these:
- crop all arrays to the size of the smallest array.
- pad all arrays to the size of the largest array (with what value?).
- re-sample all arrays to the largest, smallest , or some other number of samples.
- do something else...
Please state how you want to concatenate your differently-sized arrays together.
Neda Deljavan
2022 年 1 月 5 日
編集済み: Neda Deljavan
2022 年 1 月 5 日
I wanna do this:
- crop all arrays to the size of the smallest array.
but I loose signeficant data points!
what's the best way in ur opinion? and how can I do it
Many many thanks for ur time.
Neda Deljavan
2022 年 1 月 5 日
I should do it when I import the data and select time point, then make a numeric matrix output and convert .txt to .mat manually
Am I right?
please correct me if I am wrong.
Stephen23
2022 年 1 月 5 日
"I should do it when I import the data and select time point, then make a numeric matrix output and convert .txt to .mat manually"
I doubt that you need to do aything "manually".
"Am I right?"
As only you know what your data represents, how it is encoded, and how it needs to be handled... only you can answer that question.
Neda Deljavan
2022 年 1 月 6 日
編集済み: Neda Deljavan
2022 年 1 月 6 日
- pad all arrays to the size of the largest array (with what value?).
- re-sample all arrays to the largest, smallest , or some other number of samples.
what's the logic behind these two ways?
how can I do this?
Thanks a billion dear Stephen for your time & help
Neda Deljavan
2022 年 1 月 13 日
Hi Stephen,
I’m filtering my data and extracting features. I’ve done for 7 similar files with same codes, but for one file I’ve got this error which is about ‘filtfilt’ function.
What should I do?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/862165/image.png)
Stephen23
2022 年 1 月 14 日
"What should I do?"
FILTFILT apparently expects its inputs to be finite. Ergo, what you should do is provide it with finite inputs.
その他の回答 (1 件)
Christian Heigele
2018 年 8 月 13 日
編集済み: Christian Heigele
2018 年 8 月 13 日
I would use cat.
If you want to list them explicitly:
A1 = rand(3,3);
A2 = rand(3,3);
A3 = rand(3,3);
A_all1 = cat(3, A1, A2, A3);
If you have them in some kind of array / parse them from files: (Thanks Steven, that is much cleaner...)
A = {};
A{1} = rand(3,3);
A{2} = rand(3,3);
A{3} = rand(3,3);
A_all2 = cat(3, A{:});
1 件のコメント
参考
カテゴリ
Help Center および File Exchange で Data Type Conversion についてさらに検索
タグ
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)