How can I suppress the 'ans' output?

When my code looks like this
function [ z ] = summie( x,y )
z=x+y;
disp(['The summie is: ', num2str(z)])
end
and I typ in the command window for example
summie(1,2)
This is displayed:
The summie is: 3
ans =
3
Is there a way to suppress the 'ans' output, so that only
The summie is: 3
is displayed?

 Accepted Answer

Sagar Damle
Sagar Damle on 19 Apr 2013
Edited: Matt J on 19 Apr 2013
Use this function :-
function summie( x,y )
z=x+y;
disp(['The summie is: ', num2str(z)])
end
That is, do not send 'z' as output. In other words,return type of your function should be 'void' to suppress the 'ans' output. Search this PDF on internet –
POLYTECHNIC UNIVERSITY Department of Financial and Risk Engineering User-Defined Functions in Matlab K. Ming Leung

2 Comments

Jeroen
Jeroen on 19 Apr 2013
Edited: Jeroen on 19 Apr 2013
This works perfectly. Thanks a lot Matt!
--EDIT: oops, I meant Sagar indeed... Therefore: thanks a lot Sagar! ;) But thank you for your help too Matt. Why can't I accept 2 answers? Know what, I'll give you my vote!
I think you meant to thank Sagar. However, consider also my solution with varargout. It is similar to Sagar's solution, but allows you to continue to have an optional output argument.

Sign in to comment.

More Answers (1)

Add a semicolon to the end of the function call
sum(1,2);

4 Comments

Incidentally, it is a bad idea to call your function "sum", since this will conflict with MATLAB's native SUM command.
Jeroen
Jeroen on 19 Apr 2013
Edited: Jeroen on 19 Apr 2013
Yes, of course, but that isn't related to my question. The function I'm using here is only an example of a possible function. But I'll change the function's name if you insist.
Secondly, I'd like to change something in my function code itself (add some extra code or something) to suppress that the 'ans' is displayed.
function varargout = summie( x,y )
z=x+y;
disp(['The summie is: ', num2str(z)])
if nargout
varargout{1}=z;
end
end
This works perfectly aswell! Thanks Matt!

Sign in to comment.

Categories

Find more on Scope Variables and Generate Names in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!