How to save variables in MATLAB function in the workspace

Good day.
I have a MATLAB function which I run from a script. I want to plot some variables in the function from my script so I want to save these variables in my workspace. Is there any way to do it other than using them as an output to the function when I call it from the script? The variables are many so I am trying to avoid too many output arguments.
Thanks now and always.

1 件のコメント

Stephen23
Stephen23 2020 年 5 月 29 日
Messing around with assignin or lots of output variables is not a good approach.
Just assign the variables to fields of a structure and return that. Simple.

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

 採用された回答

Robin Kirsch
Robin Kirsch 2020 年 5 月 29 日

0 投票

Use assignin command
function y = test()
assignin('base','x',42)
y = 0
end

4 件のコメント

ojonugwa adukwu
ojonugwa adukwu 2020 年 5 月 29 日
Thanks. But this assignin assumes the variables are in the workspace already I guess? What I want is that the variables in the function can be read from the script meaning, they appear on the workspace where I can plot them from.
Thanks.
Robin Kirsch
Robin Kirsch 2020 年 5 月 29 日
assignin('base','x',42) will create a variable x in the base workspace and fill it with the number 42.
Robin Kirsch
Robin Kirsch 2020 年 5 月 29 日
the workspace can be empty and it will save it to it
Robin Kirsch
Robin Kirsch 2020 年 5 月 29 日
this works only if you run your function first and then run your script to plot the variables of course

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

その他の回答 (1 件)

Cris LaPierre
Cris LaPierre 2020 年 5 月 29 日

1 投票

You could save them to a mat file. When you want to plot them, load the mat file in your script.
You could also consider placing the variables in a structure. That way, your function only needs to have one output, yet all the variables can be passed out.

9 件のコメント

ojonugwa adukwu
ojonugwa adukwu 2020 年 5 月 29 日
Thanks Bro.
But this safe to mat file assumes the variables are in the workspace already I guess? What I want is that the variables in the function can be read from the script meaning, they appear on the workspace where I can plot them from.
Thanks.
Cris LaPierre
Cris LaPierre 2020 年 5 月 29 日
You'd have to run the save command from inside your function.
ojonugwa adukwu
ojonugwa adukwu 2020 年 5 月 29 日
Thanks.
I did. I wrote save (gas) after last line in my fucntion but I got an error message that "Undefined function or variable 'gas'.". Is there anything I need to do again?
Many thanks.
Cris LaPierre
Cris LaPierre 2020 年 5 月 29 日
編集済み: Cris LaPierre 2020 年 5 月 29 日
Look at the syntax in the link I shared. When using parentheses, you need to put your filename in quotes. Either of the following syntaxes should work:
save('gas.mat')
or
save gas.mat
I guess you don't technically need to include the extension, but it's recommended.
ojonugwa adukwu
ojonugwa adukwu 2020 年 5 月 29 日
Thanks so much Cris.
This looks promising to work. The issue now is the length of time it takes. For example, I have not completed one simulation cycle for a program that has 150 simulation cycle. I even used a variable that is a scalar here yet I waited for over an hour without completing one simulation cycle.
Cris LaPierre
Cris LaPierre 2020 年 5 月 30 日
Hard to say without seeing your code.
ojonugwa adukwu
ojonugwa adukwu 2020 年 6 月 1 日
Yea. My codes are lengthy. A script and four functions. But I have resolve it using the idea proposed by
Stephen Cobeldick (assignin). It takes time but it ok for me until I get a better one.
Many thanks for everything.
Cris LaPierre
Cris LaPierre 2020 年 6 月 1 日
Just to clarify, that wasn't Stephen's suggestion. His recommendation was NOT to do that. He suggested using a structure, as did I. That would likely be the quickest approach since you don't have to write the variables to the workspace or the harddrive.
ojonugwa adukwu
ojonugwa adukwu 2020 年 6 月 1 日
Sorry. I meant Robin Kirsch. The "assignin" he suggested is slow but it is faster than the process of saving to matfile. I really do not know why that has been extremely slow for me. Hopefully I will get to know why.
Thanks so much

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

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by