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 comentarios

is this a Simulink question ?
madhan ravi
madhan ravi el 8 de Feb. de 2019
If you are talking about simulink look into simout block.
Sky Scrapper
Sky Scrapper el 8 de Feb. de 2019
Editada: Sky Scrapper el 8 de Feb. de 2019
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.
Sky Scrapper
Sky Scrapper el 8 de Feb. de 2019
sorry, your link is not working.
madhan ravi
madhan ravi el 8 de Feb. de 2019
Why not use save() ?
Sky Scrapper
Sky Scrapper el 8 de Feb. de 2019
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
Sky Scrapper
Sky Scrapper el 8 de Feb. de 2019
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.

Iniciar sesión para comentar.

 Respuesta aceptada

Omer Yasin Birey
Omer Yasin Birey el 8 de Feb. de 2019
Editada: Omer Yasin Birey el 8 de Feb. de 2019

0 votos

14 comentarios

Sky Scrapper
Sky Scrapper el 8 de Feb. de 2019
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.
Omer Yasin Birey
Omer Yasin Birey el 8 de Feb. de 2019
Editada: Omer Yasin Birey el 8 de Feb. de 2019
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
Sky Scrapper
Sky Scrapper el 8 de Feb. de 2019
Editada: Sky Scrapper el 8 de Feb. de 2019
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?
Omer Yasin Birey
Omer Yasin Birey el 8 de Feb. de 2019
Editada: Omer Yasin Birey el 8 de Feb. de 2019
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.
Sky Scrapper
Sky Scrapper el 8 de Feb. de 2019
Editada: Sky Scrapper el 8 de Feb. de 2019
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.
Sky Scrapper
Sky Scrapper el 8 de Feb. de 2019
while i am using:
save('finaloutput.mat','p','q','-append')
it is showing error that 'q' has not is not a valid variable name.
Omer Yasin Birey
Omer Yasin Birey el 8 de Feb. de 2019
Editada: Omer Yasin Birey el 8 de Feb. de 2019
That has to be something else, q is a valid variable name. Maybe, you are missing an apostrophe between the commas or something.
Sky Scrapper
Sky Scrapper el 8 de Feb. de 2019
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.
Sky Scrapper
Sky Scrapper el 8 de Feb. de 2019
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
Sky Scrapper
Sky Scrapper el 8 de Feb. de 2019
ok thanks a lot.
Stephen23
Stephen23 el 8 de Feb. de 2019
"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:

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Etiquetas

Preguntada:

el 8 de Feb. de 2019

Comentada:

el 8 de Feb. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by