Réponse acceptée

Stephen23
Stephen23 le 28 Sep 2016

18 votes

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 commentaires

Hazel Sialongo
Hazel Sialongo le 28 Sep 2016
I want to add data not to overwrite the data.
Stephen23
Stephen23 le 28 Sep 2016
Modifié(e) : Stephen23 le 28 Sep 2016
"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:
Hazel Sialongo
Hazel Sialongo le 28 Sep 2016
But the data is from the gui edit toolbox.
blues
blues le 23 Oct 2019
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?
Stephen23
Stephen23 le 24 Oct 2019
@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.
blues
blues le 24 Oct 2019
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.
Stephen23
Stephen23 le 25 Oct 2019
@blues: please ask this in a new question.
Yahya Barghash
Yahya Barghash le 12 Avr 2020
thanks stephen you are a hero
zein
zein le 27 Avr 2021
how to add data to the saved variable without overwrites the old saved data?
Abhishek Kumar
Abhishek Kumar le 25 Août 2021
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by