How do I combine 2d matrices mat files into a single 3d matrices file?
47 次查看(过去 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?
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
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:
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?
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!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 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 (한국어)