You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
How do I combine 2d matrices mat files into a single 3d matrices file?
47 views (last 30 days)
Show older comments
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 Comments
Stephen23
on 13 Aug 2018
@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
on 13 Aug 2018
@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.
Accepted Answer
Stephen23
on 13 Aug 2018
Edited: Stephen23
on 13 Aug 2018
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 Comments
college student
on 9 Jul 2021
Edited: college student
on 9 Jul 2021
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?
Stephen23
on 9 Jul 2021
@college student: I cannot do anything with a screenshot. If you want help, upload your data in a .mat file.
college student
on 9 Jul 2021
Edited: college student
on 9 Jul 2021
list of matrix I've tried to combine. the filedname is data_matrix
Neda Deljavan
on 4 Jan 2022
*******SOS********
@Stephen
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
on 4 Jan 2022
@Neda Deljavan: are the .MAT file well-designed (with exactly the same variable names) or badly-designed (with different variable names) ?
Neda Deljavan
on 5 Jan 2022
@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
on 5 Jan 2022
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
on 5 Jan 2022
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
on 5 Jan 2022
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
on 5 Jan 2022
Edited: Stephen23
on 5 Jan 2022
"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
on 5 Jan 2022
Edited: Neda Deljavan
on 5 Jan 2022
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
on 5 Jan 2022
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
on 5 Jan 2022
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
on 5 Jan 2022
"''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:
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
on 5 Jan 2022
Edited: Neda Deljavan
on 5 Jan 2022
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
on 5 Jan 2022
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
on 5 Jan 2022
"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
on 6 Jan 2022
Edited: Neda Deljavan
on 6 Jan 2022
- 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
on 13 Jan 2022
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?
Stephen23
on 14 Jan 2022
"What should I do?"
FILTFILT apparently expects its inputs to be finite. Ergo, what you should do is provide it with finite inputs.
More Answers (1)
Christian Heigele
on 13 Aug 2018
Edited: Christian Heigele
on 13 Aug 2018
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 Comment
See Also
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)