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
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.
채택된 답변
추가 답변 (1개)
Cris LaPierre
2020년 5월 29일
1 개 추천
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
2020년 5월 29일
Cris LaPierre
2020년 5월 29일
You'd have to run the save command from inside your function.
ojonugwa adukwu
2020년 5월 29일
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
2020년 5월 29일
Cris LaPierre
2020년 5월 30일
Hard to say without seeing your code.
ojonugwa adukwu
2020년 6월 1일
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
2020년 6월 1일
카테고리
도움말 센터 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!