save or write variables in function file

Hi,
How do I actually save variables that are in a function file?

댓글 수: 1

Jan
Jan 2011년 12월 28일
Please explain, what you want to achieve.

댓글을 달려면 로그인하십시오.

답변 (3개)

Walter Roberson
Walter Roberson 2011년 12월 28일

1 개 추천

You cannot do this without altering the function file (or at least one function or script that it calls directly.)
You can, as Titus indicates, use assignin() in the function file. Better, though, would be to return the variables from the function and do the assignment outside of the function.

댓글 수: 3

Matt Tearle
Matt Tearle 2011년 12월 28일
Seconded. Using assignin completely destroys any readability or tracibility of your code. Magicking variables into existence is not recommended. Returning the values as outputs from the function is much clearer and safer.
Titus Edelhofer
Titus Edelhofer 2011년 12월 28일
Of course. The original question did not make me think about what the OP really might have wanted to do ...

댓글을 달려면 로그인하십시오.

Titus Edelhofer
Titus Edelhofer 2011년 12월 28일

0 개 추천

Use the function save? ;-)
Titus

댓글 수: 2

Aishah
Aishah 2011년 12월 28일
wouldnt this just save the variable as a mat file?
I have some variables that are in a function file that I want to bring to the workspace.
Titus Edelhofer
Titus Edelhofer 2011년 12월 28일
Yes. You can either load them in the base workspace later or use assignin, if you want to bring variables from your function to the base workspace directly.

댓글을 달려면 로그인하십시오.

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2020년 9월 5일

0 개 추천

You can save in two different ways:
  1. Assign the output variables, e.g.: function [Out1, Out2, Out3, Outn] = MYfunction(In1, In2, In3, InN)
function [Out1, Out2, Out3, Outn] = MYfunction(In1, In2, In3, InN)
% that saves the outputs Out1, Out2, Out3, Out4 when you recall and execute the function file called: MYfunction.
Out1 = In1+In2;
Out2 = In3/In1;
Out3 = In1/In2;
OutN = InN/(In1+In2+In3);
end
2. Use and set up: assignin() % Not recommended for a few good reasons
e.g.: assignin('base','Output1', Out1)

카테고리

태그

질문:

2011년 12월 28일

댓글:

2020년 9월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by