How to find out the output variable "ans" of exactly which statement is?

2 views (last 30 days)
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!

Accepted Answer

Jan
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
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.
D.Chehov
D.Chehov on 13 Mar 2012
Well,I've found out that the unwanted output "ans=" is the same as the outcome of the line:
A=blktridiag(Amd,Asub,Asup,n);
e.g. disp (A);
I'd like to clear up, that the function "blktridiag" was taken from this forum,wherefore again thanks to its author. I modified it a litle for my case and tried in stand alone mode,where the outputs are named one!
Than I used it as a subfunction and the "ans=" appears....
I tried debugging and "echo" command ,they help...but in fact all I want at this step of the solution is to suppres the "ans.." output

Sign in to comment.

More Answers (2)

Image Analyst
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"

Image Analyst
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.

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!