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 件のコメント

Walter Roberson
Walter Roberson 2019 年 2 月 8 日
is this a Simulink question ?
madhan ravi
madhan ravi 2019 年 2 月 8 日
If you are talking about simulink look into simout block.
Sky Scrapper
Sky Scrapper 2019 年 2 月 8 日
編集済み: Sky Scrapper 2019 年 2 月 8 日
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 2019 年 2 月 8 日
sorry, your link is not working.
madhan ravi
madhan ravi 2019 年 2 月 8 日
Why not use save() ?
Sky Scrapper
Sky Scrapper 2019 年 2 月 8 日
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.
madhan ravi
madhan ravi 2019 年 2 月 8 日
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 2019 年 2 月 8 日
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.''
Walter Roberson
Walter Roberson 2019 年 2 月 8 日
Do not save naming .m file as destination . Name a .mat file.

サインインしてコメントする。

 採用された回答

Omer Yasin Birey
Omer Yasin Birey 2019 年 2 月 8 日
編集済み: Omer Yasin Birey 2019 年 2 月 8 日

0 投票

14 件のコメント

Sky Scrapper
Sky Scrapper 2019 年 2 月 8 日
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 2019 年 2 月 8 日
編集済み: Omer Yasin Birey 2019 年 2 月 8 日
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 2019 年 2 月 8 日
編集済み: Sky Scrapper 2019 年 2 月 8 日
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 2019 年 2 月 8 日
編集済み: Omer Yasin Birey 2019 年 2 月 8 日
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 2019 年 2 月 8 日
編集済み: Sky Scrapper 2019 年 2 月 8 日
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?
Omer Yasin Birey
Omer Yasin Birey 2019 年 2 月 8 日
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 2019 年 2 月 8 日
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 2019 年 2 月 8 日
編集済み: Omer Yasin Birey 2019 年 2 月 8 日
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 2019 年 2 月 8 日
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?
Omer Yasin Birey
Omer Yasin Birey 2019 年 2 月 8 日
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 2019 年 2 月 8 日
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?
Omer Yasin Birey
Omer Yasin Birey 2019 年 2 月 8 日
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 2019 年 2 月 8 日
ok thanks a lot.
Stephen23
Stephen23 2019 年 2 月 8 日
"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:

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeSimulink についてさらに検索

製品

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by