필터 지우기
필터 지우기

Concatenation of multiple matfile into one matfile

조회 수: 9 (최근 30일)
MEHDI HAMIDI
MEHDI HAMIDI 2022년 4월 8일
댓글: MEHDI HAMIDI 2022년 4월 8일
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
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?
MEHDI HAMIDI
MEHDI HAMIDI 2022년 4월 8일
my files contains 9 columns and and several lines and it's only numbers e.g line 1 : 1 2 3 4 6 7 8 9,
line 2 : 1 9 8 8 2 3 7 8 9, i have 75 files the order doesnt matter, thank you sir

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
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
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.
MEHDI HAMIDI
MEHDI HAMIDI 2022년 4월 8일
Nevermind, it actually works thanks a lot

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Benjamin Thompson
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
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]
MEHDI HAMIDI
MEHDI HAMIDI 2022년 4월 8일
Thanks you sir

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile 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!

Translated by