How to automatically concatenate several .mat files into one file?
Mostrar comentarios más antiguos
Here is what I am trying to do:
I have these four .mat files... '20130602_SPLBRENT3_135638.mat' '20130602_SPLBRENT3_183333.mat' '20130602_SPLBRENT3_184002.mat' '20130602_SPLBRENT3_205239.mat'
I want to write perhaps a script or function that will concatenate those four files into one file with the name '20130602_SPLBRENT3_concat.mat' My goal is to be able to automate this process through Task Scheduler so that the files are concatenated once a day. As you can see, these are files from June 2.
Anyone know how I go about doing this? I am very new to Matlab so please be as detailed as possible. Thanks!
4 comentarios
Sean de Wolski
el 3 de Jun. de 2013
How is this different than:
Lianna Johnson
el 3 de Jun. de 2013
Iain
el 3 de Jun. de 2013
I think an important question is why do you want to concatenate those files?
Walter Roberson
el 3 de Jun. de 2013
Respuestas (1)
Iain
el 3 de Jun. de 2013
Mat files aren't easy to concatenate UNLESS they contain different variables.
The code, in that case, would be:
load(filename1)
load(filename2)
load(filename3)
load(filename4)
save(filename)
If they have similar variables inside them, then concatenation is harder, but you can bodge it easily:
a{1} = load(filename1);
a{2} = load(filename2);
a{3} = load(filename3);
a{4} = load(filename4);
save(filename,a)
4 comentarios
Walter Roberson
el 3 de Jun. de 2013
Before doing the save,
a = vertcat(a{:});
Then the save would be
save(filename, '-struct', 'a');
This will only work if the variable names are exactly the same, in the same order, in all of files.
Iain
el 4 de Jun. de 2013
And if they are structures, defined in the same way...
Walter Roberson
el 4 de Jun. de 2013
load() of a .mat file returns a structure whose fieldnames are the original variable names.
But I do need to think more about what should be concatenated together. The above will probably fail because -struct in save needs scalar structures.
Walter Roberson
el 4 de Jun. de 2013
For the case of .mat files with exactly one variable name:
t1 = load(filename1);
t2{1} = struct2cell(t);
t2{2} = struct2cell(load(filename2));
t2{3} = struct2cell(load(filename3));
t2{4} = struct2cell(load(filename4));
fn = fieldnames(t1);
t3.(fn{1}) = vertcat(t2{:});
save(filename, '-struct', 't3');
Categorías
Más información sobre Workspace Variables and MAT Files en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!