How can I append the data available in two mat files with some variables having the same name, into a single file?

6 views (last 30 days)
I have two mat files matlab1.mat and matlab2.mat which have variables with the same names. I want to append the data of these common variables in matlab1.mat to the data in matlab2.mat.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
The attached MATLAB file "append_var.m" is an example of how you can achieve this functionality. Also download the two sample MAT files attached to execute this example. The process involves the following steps:
1. Load the first MAT file (matlab1.mat) and read all the variables into another variable, say “file1” (stored as STRUCT).
2. Next read all the variables in the second MAT file (matlab2.mat) into another variable, say “file2” using the WHOS command without actually loading them into the workspace.
3. Do a string compare to see which variables are common between the two MAT files. If there is a common variable name, save the variable in current MAT file to temp, and load the variable from the other MAT file into the workspace and append it to temp using the EVAL function. If not, at the end of the loop just load this variable into the workspace.
4. Finally, clear the unwanted variables and save the current set of variables in another MAT file (if needed) or just overwrite to another file and delete any unnecessary MAT files.

More Answers (0)

Products


Release

R14SP1

Community Treasure Hunt

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

Start Hunting!