Why in a function an output argument isn't saved to workspace?

조회 수: 29 (최근 30일)
Student Israeli
Student Israeli 2021년 4월 24일
댓글: Student Israeli 2021년 4월 24일
function This_is_the_output_I_want_to_save_to_workspace = MyFunc(~)
My_input = input ('Enter input here: ');
This_is_the_output_I_want_to_save_to_workspace = My_input^2;
end
  댓글 수: 2
Jonas
Jonas 2021년 4월 24일
variable names you set inside a function don't carry over to the workspace in which you call the function. you have to assign the output of your function to a variable with the name you asked for
Rik
Rik 2021년 4월 24일
Matlab stored it in the ans variable, just like you asked it to do.

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

채택된 답변

John D'Errico
John D'Errico 2021년 4월 24일
편집: John D'Errico 2021년 4월 24일
Suppose you use ANY MATLAB function? How does it work? For example...
x = 1:5;
Suppose you want to compute the mean of that vector? Do you do it like this?
mean(x);
If you do, then where does the result get stored?
whos
Name Size Bytes Class Attributes ans 1x1 8 double x 1x5 40 double
If you look, the result got dumped unceremoniously into ans. You need to put it INTO somethign to get stored. The name in the function definition does nothing, except tell MATLAB which variable to return.
If you want to put it into something, then you do this:
result = mean(x);
whos
Name Size Bytes Class Attributes ans 1x1 8 double result 1x1 8 double x 1x5 40 double
Now a result exists in a variable with my choice of name, NOT in ans.
When you return a variable, you need to remember the name of that variable when inside the function is not retained. The outside world does not know anything about the variable names inside your function.
  댓글 수: 4
John D'Errico
John D'Errico 2021년 4월 24일
편집: John D'Errico 2021년 4월 24일
YOU DON'T!!!!!!!
You do not assign the name of a variable in the output workpace from inside a function!
You PUT the result INTO the variable you want it to be in, when you call the function. You do this by assigning it to some variable when you call it. Surely whenever you call the function mean, for example, you do not want it to ALWAYS create the result in some named variable, and ALWAYS the same name?
If you want the name of the variable to be This_is_the_output_I_want_to_save_to_workspace, then you call it like this, WHEN you call the function:
This_is_the_output_I_want_to_save_to_workspace = MyFunc;
But the next time you call that function, you may want the name in the caller workspace to be something else. So you may now do this:
Fred = myfunc;
or
Barney = myfunc;
You call the output ANYTHING you want. The caller workspace does not know anything about the variable names inside your function, nor should it. Again, every variable that was inside your function disappears after that function terminates. That includes the internal variable names.
(Actually, with some effort and using a tool that you do not know about, nor do you really want to learn, you CAN assign a variable name in the caller workspace. This is not even remotely good programming practice however.)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by