Concatenation of multiple matfile into one matfile
조회 수: 4 (최근 30일)
이전 댓글 표시
Hi Hello everyone, i have multiples mat files and i would like to concatenate them (merge vertically) in one matfile, all my matfiles have values only, (9 columns and several lines,), the output should be 1 file with 9 columns and all the lines from the previous multiples matfiles
댓글 수: 2
Matt J
2022년 4월 8일
Do all the files contain only a single variable? Is the name of the variable the same in all the files? How many files and what determines the order of the concatenation?
채택된 답변
Stephen23
2022년 4월 8일
Assuming that each .mat file contains only one variable of unknown name:
P = 'absolute or relative path to where the files are saved';
S = dir(fullfile(P,'*.mat'));
for k = 1:numel(S)
F = fullfile(P,S(k).name);
C = struct2cell(load(F));
S(k).data = C{1};
end
M = vertcat(S.data);
save('newfile.mat','M')
댓글 수: 3
Stephen23
2022년 4월 8일
@MEHDI HAMIDI: the code I gave you only saves matrix M, because that is contains your concatenated data. You do not need the other variables.
추가 답변 (1개)
Benjamin Thompson
2022년 4월 8일
MAT files contain a list of variables with your data. Please post a sample MAT file for the Community to see if you have further questions. You could use the structure return syntax from the load function:
S = load(FILENAME) loads the variables from a MAT-file into a structure
array, or data from an ASCII file into a double-precision array.
Specify FILENAME as a character vector or a string scalar. For example,
specify FILENAME as 'myFile.mat' or "myFile.mat".
S1 = load(FILENAME1);
S2 = load(FILENAME2);
Then you could combine the data in structures S1, S2, etc into a final structure or some other form.
댓글 수: 3
Benjamin Thompson
2022년 4월 8일
Ok, so you load it:
>> S1 = load('Snap1_0_360.mat')
S1 =
struct with fields:
OmniPDP: [10×9 double]
You can copy S1 to a new Stotal that might receive the contents of other files:
>> Stotal = S1
Stotal =
struct with fields:
OmniPDP: [10×9 double]
And this example you could easily replace the second copy of S1 with some of your other files like S2. Here I just show how to create a combined OmniPDP member in Stotal using S1 twice.
>> Stotal.OmniPDP = [S1.OmniPDP; S1.OmniPDP];
>> Stotal
Stotal =
struct with fields:
OmniPDP: [20×9 double]
참고 항목
카테고리
Help Center 및 File Exchange에서 Workspace Variables and MAT-Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!