Add new data into existing mat file

 Accepted Answer

The save documentation gives this syntax:
save(filename,variables,'-append')
and describes it as " save(filename,variables,'-append') adds new variables to an existing file. If a variable already exists in a MAT-file, then save overwrites it. The variables argument is optional."

11 Comments

I want to add data not to overwrite the data.
"I want to add data not to overwrite the data."
Lets read the line from the documentation more carefully then: "adds new variables to an existing file"
Here is an example of using the -append option, exactly like my answer states:
>> X = 1:3;
>> Y = 4:6;
>> save('test','X','Y')
>> Z = 7:9;
>> save('test','Z','-append')
>> S = load('test.mat')
S =
X: [1 2 3]
Y: [4 5 6]
Z: [7 8 9]
So the answer that I gave you worked perfectly: it appended the new data without overwriting anything. Did you actually try it? Does this do what you want?
If you want to append data to an array in the mat file then the easiest way is to use matfile, which lets you "Access and change variables directly in MAT-files, without loading into memory", as is explained here:
But the data is from the gui edit toolbox.
Hi Stephen,
What if I want to add a data to the existing variables X and Y in your example? Let's say, I want to add X = 10:15; and Y = 20:25; in the test.mat file, how can I do this?
@blues : either:
  1. load, concatenate, save, or
  2. use matfile.
If using option 1. remember to load into an output variable rather than directly into the workspace.
Hi Stephen,
Could you give me suggestion on the following example?
I have a data in the following format: (i, j, k, val) where i, j, k are variables and val is the corresponding data at i, j, k values. I want to save the .mat file in the (i, j, k, val) format, so that my .mat files looks like:
(0, 0, 0, 1.5E-02)
(0, 0, 1, 2.5E-05)
...........................
(5, 5, 5, 4.5 E-05)
How can I create this type of .mat file?
Actually these data are the octant of a 11 by 11 by 11 matrix (isotropic cubic matrix). I would like to have a full-size matrix using the symmetry property of the octant. I want to save the octant data in a .mat file so that I could iterate over the 8 possibilities to make a full size matrix.
@blues: please ask this in a new question.
thanks stephen you are a hero
how to add data to the saved variable without overwrites the old saved data?
I have the same problem, I don't want it to overwrite either, I need to save multiple outputs from different run to the same file.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!