Concatenation of multiple matfile into one matfile
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
MEHDI HAMIDI
el 8 de Abr. de 2022
Comentada: MEHDI HAMIDI
el 8 de Abr. de 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 comentarios
Matt J
el 8 de Abr. de 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?
Respuesta aceptada
Stephen23
el 8 de Abr. de 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 comentarios
Stephen23
el 8 de Abr. de 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.
Más respuestas (1)
Benjamin Thompson
el 8 de Abr. de 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 comentarios
Benjamin Thompson
el 8 de Abr. de 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]
Ver también
Categorías
Más información sobre Workspace Variables and MAT-Files en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!