How to save variables in MATLAB function in the workspace
76 ビュー (過去 30 日間)
古いコメントを表示
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
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
2020 年 5 月 29 日
Use assignin command
function y = test()
assignin('base','x',42)
y = 0
end
4 件のコメント
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
2020 年 5 月 29 日
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 件のコメント
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.
参考
カテゴリ
Help Center および File Exchange で Entering Commands についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!