How to save matlab coding output data in a file?

Hello,
I am working with matlab coding. I need to run the simulations for different models. For the 1st model, I run the simulation, it saves output in the workspace. but when i run the 2nd model, it saves the output file for the 2nd model. That is why i need to save the output for the 1st model to a file. So, i can store my outputs of all models.
can anyone please suggest me how to do?

11 Comments

is this a Simulink question ?
If you are talking about simulink look into simout block.
no, not simulink. it's matlab coding file using matlab editor.
say for the 1st model, I have data saved on workspace:
output= 100x10 matrix
P= 22x1 matrix
Q= 7x1 matrix.
For the model 2, i have,
output= 230x8 matrix
P= 12x1 matrix
Q= 7x1 matrix.
If I run the second model, then I will lost all the data for the 1st model saved on the matlab workspace window. That why I want to save the data of the 1st model to a file so that I will get all the data while I am working with another model.
sorry, your link is not working.
Why not use save() ?
My file name is: for 1st model: final output
for 2nd model: output
I am writing:
save (' finaloutput.mat',' output','P')
it is working but i do not know where it is saving.
It saves in a file named finaloutput.mat to use the values simply use
load finaloutput.mat % variables will be loaded in workspace
it's showing error:
''Error using load
Number of columns on line 3 of ASCII file finaloutput.m must be the same as previous lines.''
Do not save naming .m file as destination . Name a .mat file.

Sign in to comment.

 Accepted Answer

Omer Yasin Birey
Omer Yasin Birey on 8 Feb 2019
Edited: Omer Yasin Birey on 8 Feb 2019

14 Comments

If I am writing:
m = matfile('finaloutput.m','Writable',true);
it's showing:
Error using matlab.internal.language.isPartialMATArrayAccessEfficient
Unable to read MAT-file C:\Users\\Desktop\finaloutput.m: not a binary MAT-file.
Hi, you should have written 'finaloutput.mat' not 'finaloutput.m'. Try this
m = matfile('finaloutput.mat','Writable',true);% now you will use m to save
m.oldVariable = variableThatYouWantToSave; %its the variable that you want to save
and after your new simulation you can add the new variable into this matfile
m.newVariable = newVariableThatYouWantToSave;%new variable
yes that is working. if i want to save the data stored in more than one variable say 'p' 'q'
then, m.oldVariable = 'p','q'
but it will not work.
then how to do?
save('finaloutput.mat','p','q','-append')
Don't forget to write '-append' at the end otherwise it will write the variables over your existing variables. Meaning that your old variables will be deleted.
it I use directly: save('finaloutput.mat','p','q','-append')
then where the file will be saved? i mean it does not show on the workspace.Then where to find the saved file?
load('finaloutput.mat')
This line above will bring you the old variable.
About the directory, the mat file will be in the folder that you work in. When you write the line above, the variables will appear in the workspace.
while i am using:
save('finaloutput.mat','p','q','-append')
it is showing error that 'q' has not is not a valid variable name.
That has to be something else, q is a valid variable name. Maybe, you are missing an apostrophe between the commas or something.
nope. i have just copied from here. is it possible to do with:
m = matfile('finaloutput.mat','Writable',true);
m.oldVariable = variableThatYouWantToSave;
this or i need to write:
m.oldVariable = variableThatYouWantToSave;
seperately for each variable?
To use matfile object, m, you have to write this line for each variable
m.oldVariable = variableThatYouWantToSave;
m.newVariable = new;% etc
But it is strange that 'q' is giving that kind of error.
it's ok. could you please let me know how to delete one variable once stored. say, i want to delete the data of 'q' variable now?
Unfortunately, matfiles doesn't have remove option. But at this link the user wrote a function for this. You can use it by copying the .m file into your folder. It seems working
"Don't forget to write '-append' at the end otherwise it will write the variables over your existing variables. Meaning that your old variables will be deleted."
Yet that is exactly what the -append option does for .mat files: it overwrites existing variables of the same name, which confuses a lot of beginners (it would be better named overwrite or something similar). This is explained in the save help (and easily checked with a few tests).
The suggested answer to use matfile is pointlessly complex for this trivial task. Much simpler to use save in a loop, generating the filename each time:

Sign in to comment.

More Answers (0)

Products

Tags

Community Treasure Hunt

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

Start Hunting!