Appending two or more .mat files

73 次查看(过去 30 天)
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

采纳的回答

Matt Tearle
Matt Tearle 2012-3-28
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 个评论
Barnabas
Barnabas 2013-11-27
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
Maha Mosalam 2021-1-17
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)

请先登录,再进行评论。

更多回答(2 个)

Thomas
Thomas 2012-1-24
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 个评论
Thomas
Thomas 2012-1-24
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]
Ferd
Ferd 2012-1-24
So as per my understanding you are saying that I should rewrite the variables to prevent overwriting and follow the same? Once changed won't the variables be stacked below? I hope I understood correctly.
Lets consider ECU_A.mat and ECU_B.mat files to be TestRun1 and TestRun2 for an engine(say). Each run will have same engine parameters and corresponding readings/values. Also, the length of all parameters for TestRun1 will be the same for the test but different from TestRun2.
I would just need to stack the TestRun2 results under TestRun1 results to get a MasterResults.mat file that combines both runs.
I can do it in Excel but the sheer length of the data is much larger than Excel's maximum limit.

请先登录,再进行评论。


C.J. Harris
C.J. Harris 2012-3-28
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];

类别

Help CenterFile Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by