Concatenation of multiple matfile into one matfile

3 views (last 30 days)
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 Comments
MEHDI HAMIDI
MEHDI HAMIDI on 8 Apr 2022
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

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 8 Apr 2022
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 Comments

Sign in to comment.

More Answers (1)

Benjamin Thompson
Benjamin Thompson on 8 Apr 2022
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 Comments

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!