How to find out the output variable "ans" of exactly which statement is?
2 views (last 30 days)
Show older comments
Hello, I have one very simple question and I will appreciate your comment!
I have primary function and couple sub function, and when I run them I obtain the solution and one more answer "ans=..", which I don't need....
If I run in stand alone mode every one of the function I don't obatian "ans=...", only the output argument that are specified. And I can't find out where in the code is the statement that gives this "ans=...".
Any advice is highly appreciate!
Thank you and regards!
0 Comments
Accepted Answer
Jan
on 8 Mar 2012
You can step through the code using the debugger. Then run through the program until the additionoutput appears in the command window.
[EDITED] Code example to suppress the automatic assignment of ans:
function reply = myFunc(data)
value = sin(data);
if nargout ~= 0
reply = value;
end
Now
myFunc(0.5)
displays the result of sin(0.5), but does not assign ans.
b = myFunc(0.5)
This assigns the result to b and display it.
c = myFunc(0.5);
Assignment, but no display.
8 Comments
Jan
on 10 Mar 2012
I've edited the program again. The reply of a function is caught in ans as a fallback. But myFunc does not create a reply if nargout is 0. Then the the variable "reply" is not initialized and not caught in consequence.
More Answers (2)
Image Analyst
on 8 Mar 2012
You need to assign it to a variable so that the variable name gets printed. So instead of something like
4*5
You'd have
a=4*5
Then it will show you the name "a" instead of just "ans"
0 Comments
Image Analyst
on 9 Mar 2012
Debugging is the best way. You can also issue the "echo on" command so that each statement is printed to the window and you can scan that (it will be looooooong if you have a lot of lines of code) and try to find "ans" buried in there.
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices 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!