2

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.

5
  • What is varout? I didn't see you define varout anywhere... So that's why Matlab complains it's undefined. Or do you mean xout? Commented Feb 12, 2016 at 6:54
  • I also think he/she means xout as it is mentioned in the textual description above. xout is only locally defined within your function testfunc. You can name these variables whatever you like they are (in general) not visible outside your function. Therefore you'd have to call it like xout = testfunc(5) (if you want to use the same variable name, which is possible). Commented Feb 12, 2016 at 7:09
  • Assume you mean xout. The variable goes out of scope at the end of the function. The return value is copied to v = testfunc(5); or to ans. You may want to read the Matlab primers. Even experienced programmers starting with Matlab should read this. Commented Feb 12, 2016 at 7:09
  • @MatthiasW. I am not sure since Matlabs implementation of return values is not that discussed, but the guess is that it is not only invisible, but not even defined outside of the function. The variable should go out of scope and the memory not even be accessible to you program anymore. Commented Feb 12, 2016 at 7:11
  • @patrik: Yes you are right, "out of scope" is the correct term. Commented Feb 12, 2016 at 7:13

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks every one.Great help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.