MATLAB Answers

How to edit matlab saved file?

3 views (last 30 days)
Zeynab Mousavikhamene
Zeynab Mousavikhamene on 3 Nov 2019
I have some tables and arrays saved as matlab file. I need to change some parts (not all) and save it.
How can I do that? When I load in the matlab and change some number from "variable" section of the matlab, it is wont save in the actual file.

  0 Comments

Sign in to comment.

Answers (1)

Bjorn Gustavsson
Bjorn Gustavsson on 3 Nov 2019
I think something like this should work:
filename2edit = 'something.mat';
load(filename2edit,'X2change')
X2change(3) = 12;
save(filename2edit,'X2change','-append')
HTH

  3 Comments

Zeynab Mousavikhamene
Zeynab Mousavikhamene on 3 Nov 2019
But I think it would append sth. I need to edit some existing part not append sth.
Walter Roberson
Walter Roberson on 3 Nov 2019
Yes, something like that can work.
Note that the entire X2change variable would be replaced when you use -append . You cannot, for example, do
filename2edit = 'something.mat';
X2change = 12;
save(filename2edit,'X2change','-append')
to end up with 12 appended at the end of the existing X2change in the file; this code would replace all of X2change with 12.
Bjorn Gustavsson
Bjorn Gustavsson on 4 Nov 2019
@Zeynab: In my example you would load the X2change variable from the file, modify it as you see fit (In my example the third component would be set to 12 (changed to 12 if it existed already, or extended X2change to a 3-element vector with 12 in the third element)), then it would save X2change back to the same file, i.e. overwriting the X2change variable with your modified variable, as Walter pointed out.
HTH

Sign in to comment.

Sign in to answer this question.

Tags


Translated by