How can I suppress the 'ans' output?

조회 수: 50 (최근 30일)
Jeroen
Jeroen 2013년 4월 18일
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?

채택된 답변

Sagar Damle
Sagar Damle 2013년 4월 19일
편집: Matt J 2013년 4월 19일
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
Jeroen
Jeroen 2013년 4월 19일
편집: Jeroen 2013년 4월 19일
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!
Matt J
Matt J 2013년 4월 19일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt J
Matt J 2013년 4월 18일
Add a semicolon to the end of the function call
sum(1,2);
  댓글 수: 4
Matt J
Matt J 2013년 4월 19일
function varargout = summie( x,y )
z=x+y;
disp(['The summie is: ', num2str(z)])
if nargout
varargout{1}=z;
end
end
Jeroen
Jeroen 2013년 4월 19일
This works perfectly aswell! Thanks Matt!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Image Processing and Computer Vision에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by