Appending two or more .mat files
43 views (last 30 days)
Show older comments
Hi
I have two .mat files ECU_1.mat and ECU_2.mat (basically both files contains same variables in level 1, of ECU_1.mat and ECU_2.mat, and the corresponding readings in level 2). I need to create a new ECU_A.mat file that would append both the .mat files into ECU_A.mat files and have all the level 1 and level 2 in the same way but with additional data of ECU_2.mat.
Thanks Ferd
0 Comments
Accepted Answer
Matt Tearle
on 28 Mar 2012
If I interpret your question correctly, you have two MAT-files containing the same variable names, but with different data, and you want to concatenate the data from each file into a new variable (with, again, the same name). For example, MAT-file #1 contains A = [1,2,3,4] and MAT-file #2 contains A = [6,7,8,9]. You want to write MAT-file with A = [1,2,3,4;6,7,8,9]. Yes?
If so,
x = load('file1');
y = load('file2');
x and y are now structures with fields given by the variable names in the MAT-files (such as x.A).
% Concatenate
x.A = [x.A;y.A];
Then save out again:
save('file3','-struct','x');
Here's a general version that doesn't care what the variables are called, as long as they're the same in each file (and they can be concatenated vertically):
% Create example data
A = magic(4);
B = magic(3);
save('ECU_1','A','B');
A = [3,2,4,1];
B = [7,8,9];
save('ECU_2','A','B');
clear('A','B')
% Combine data from two MAT-files
x = load('ECU_1');
y = load('ECU_2');
% Check to see that both files contain the same variables
vrs = fieldnames(x);
if ~isequal(vrs,fieldnames(y))
error('Different variables in these MAT-files')
end
% Concatenate data
for k = 1:length(vrs)
x.(vrs{k}) = [x.(vrs{k});y.(vrs{k})];
end
% Save result in a new file
save('ECU_A','-struct','x')
3 Comments
Barnabas
on 27 Nov 2013
If you had more than two files to append, say I have 15. What would the syntax be for the if statement when checking the fieldnames, I would want to check them against all 15 files? Also, what would the syntax be for the for loop? Thank you
Maha Mosalam
on 17 Jan 2021
what if the files contain matrices , so i want to combine for example
file1 :contains 1000 A matrices >> A(:,:,1:1000) and
file 2: contains another 1000 A matrices >> A(:,:,1001:2000)
how I can combine these matrices from the two files so, I can had one file contains all matrices A(:,:,1:2000)
More Answers (2)
Thomas
on 24 Jan 2012
load both the *.mat files with the variables, append them in matlab and save them using save command
load ECU_A.mat % suppose this has variables a & b
load ECU_B.mat % suppose this has variables c & d
% assuming all variables of the same length and are corresponding
new_data=[a b c d] % if you need them in a single matrix
save ECU_A.mat
3 Comments
Thomas
on 24 Jan 2012
If the variable from both mat files has the same name, you need to rename them after you load the first mat file otherwise they will be overwritten in the variable space.
then you can load the second mat file and append it to the bottom of the first.
example if you have the four variables a,b,a1 and b1 in your workspace and you want to append a1 underneath a and b1 underneath b you would use some thing like:
load ECU_A.mat % suppose this has variables a & b
load ECU_B.mat % suppose this has variables a1 & b1
c=[a;a1];
d=[b;b1];
new_data=[c d]
C.J. Harris
on 28 Mar 2012
You can also load data into a structure, and manipulate it from there. For example:
FILE_A = load('ECU_1.mat');
FILE_B = load('ECU_2.mat');
Then if a variable exists in both you could join them in any way you like:
A_COMBINED = [FILE_A.A; FILE_B.A];
0 Comments
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!