Can I supress an 'ans' output without making a void function?

2 ビュー (過去 30 日間)
Charles Denis
Charles Denis 2014 年 9 月 22 日
コメント済み: Guillaume 2014 年 9 月 22 日
function[ Hours Minutes Seconds Message ] = TimeConversion(TotalSeconds);
Hours = floor(TotalSeconds/3600);
Minutes = floor((TotalSeconds-(Hours*3600))/60);
Seconds = TotalSeconds-Hours*3600-Minutes*60;
Message = [num2str(TotalSeconds) ' Second(s) is equal to '... num2str(Hours) ' hour(s), ' ... num2str(Minutes) ' minute(s), and ' ... num2str(Seconds) ' second(s).'];
disp(Message);
end
When I Run this code:
>> TimeConversion(51624)
51624 Second(s) is equal to 14 hour(s), 20 minute(s), and 24 second(s).
ans =
14
Is there a way to suppress the ans?(which is obviously referencing 'Hours'), but keep the array in the function output for things such as: "[out1, out3] = TimeConversion(82000)"

採用された回答

Guillaume
Guillaume 2014 年 9 月 22 日
Yes, test to see if nargout is 0 and if it is don't assign anything to any of the outputs. You'll have to rejig your code to use temporary variables for hours, minute, seconds and message:
function [Hours, Minutes, Seconds, Message] = TimeConversion(TotalSeconds);
h = floor(TotalSeconds/3600);
m = floor((TotalSeconds-(Hours*3600))/60);
s = TotalSeconds-Hours*3600-Minutes*60;
msg = sprintf('%d Seconds is equal to %d hour(s), %d minute(s), and %d second(s).', TotalSeconds, h, m, s);
disp(msg);
if nargout > 0
Hours = h;
Minutes = m;
Seconds = s;
Message = msg;
end
end
  3 件のコメント
José-Luis
José-Luis 2014 年 9 月 22 日
Not so sure about that. The "standard" way, if there is such a thing, would be to use a semi-colon. Here, you are just introducing unnecessary overheads, IMO.
Guillaume
Guillaume 2014 年 9 月 22 日
matlab does something similar in one of their function. Can't remember which one.
Personally, I would avoid changing the behaviour of the function based on the number of output, so I wouldn't use this construct.

サインインしてコメントする。

その他の回答 (1 件)

A Jenkins
A Jenkins 2014 年 9 月 22 日
Use a semicolon.
TimeConversion(51624);
or
[out1, out3] = TimeConversion(82000);

カテゴリ

Help Center および File ExchangeWhos についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by