Filter löschen
Filter löschen

Concatenation of multiple matfile into one matfile

9 Ansichten (letzte 30 Tage)
MEHDI HAMIDI
MEHDI HAMIDI am 8 Apr. 2022
Kommentiert: MEHDI HAMIDI am 8 Apr. 2022
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 Kommentare
Matt J
Matt J am 8 Apr. 2022
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 am 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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Stephen23
Stephen23 am 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 Kommentare
Stephen23
Stephen23 am 8 Apr. 2022
@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 am 8 Apr. 2022
Nevermind, it actually works thanks a lot

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Benjamin Thompson
Benjamin Thompson am 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 Kommentare
Benjamin Thompson
Benjamin Thompson am 8 Apr. 2022
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]

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Workspace Variables and MAT-Files finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by