Reputation: 123
I have written a function using matlab as follows.
function xout= testfunc(b)
xout = b;
end
I executed this function by giving an value for b as follows.
addpath ('C:\Users\vish\docs\copy');% this is the place of the file location
testfunc(5);
This gives me a variable in my work space ans which is equal to 5,which is the general variable once after all functions are evoked.But the problem is,even though the function is executed properly, when try to use the variable xout it gives the following error message.
addpath ('C:\Users\vish\docs\copy');
testfunc(5);
varout
Undefined function or variable 'varout'.
Any help is highly appreciated.
Upvotes: 2
Views: 852
Reputation: 3177
By default, if you do not assign a function returned value to a variable, Matlab assigns it to ans
(as you already experienced). By calling
testfunc(5)
Matlab will create a variable in the workspace called ans
with value 5
.
Also the name xout
is the output variable name in the body of your function (i.e. in the testfunc
script the output variable name is known as xout
) but when you call such function you can assign its output to any variable (i.e. with the name you prefer).
myNewOutput=testfunc(5)
and myNewOutput
in the workspace will have value 5
. Or you can as well use the same name you used in the function, is up to you:
xout=testfunc(5)
and xout
will have value 5
as well.
Upvotes: 2