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

32 views (last 30 days)
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 Comments
Jonas
Jonas on 24 Apr 2021
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 on 24 Apr 2021
Matlab stored it in the ans variable, just like you asked it to do.

Sign in to comment.

Accepted Answer

John D'Errico
John D'Errico on 24 Apr 2021
Edited: John D'Errico on 24 Apr 2021
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 Comments
John D'Errico
John D'Errico on 24 Apr 2021
Edited: John D'Errico on 24 Apr 2021
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.)

Sign in to comment.

More Answers (0)

Categories

Find more on Debugging and Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!