How do I combine 2d matrices mat files into a single 3d matrices file?

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

You do not assign values at any point. How do you load your .mat files?
@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?
@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.

Sign in to comment.

 Accepted Answer

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: replace fieldname with the name of the required field in T (which is the name of the variable in the .mat files).
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?
@college student: I cannot do anything with a screenshot. If you want help, upload your data in a .mat file.
list of matrix I've tried to combine. the filedname is data_matrix
*******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?
@Neda Deljavan: are the .MAT file well-designed (with exactly the same variable names) or badly-designed (with different variable names) ?
@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
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!
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!
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''
"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.
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
how can I be in contact with you in emercencies?
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')
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 :((((((
"''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:
  1. crop all arrays to the size of the smallest array.
  2. pad all arrays to the size of the largest array (with what value?).
  3. re-sample all arrays to the largest, smallest , or some other number of samples.
  4. do something else...
Please state how you want to concatenate your differently-sized arrays together.
I wanna do this:
  1. 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.
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.
"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.
  1. pad all arrays to the size of the largest array (with what value?).
  2. 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
"what's the logic behind these two ways?"
  1. append or prepend some value (e.g. NaN, zero) to the arrays, e.g. using ZEROS or NAN and concatenation.
  2. Use INTERP1, RESAMPLE, or similar to create new data points based on the existing data points.
@Stephen
I will try to pass this huge problem.
Thanks a lot
Bests
Neda
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?
"What should I do?"
FILTFILT apparently expects its inputs to be finite. Ergo, what you should do is provide it with finite inputs.

Sign in to comment.

More Answers (1)

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{:});

Categories

Community Treasure Hunt

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

Start Hunting!