Can I supress an 'ans' output without making a void function?
1 view (last 30 days)
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).'];
When I Run this code:
51624 Second(s) is equal to 14 hour(s), 20 minute(s), and 24 second(s).
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 on 22 Sep 2014
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);
if nargout > 0
Hours = h;
Minutes = m;
Seconds = s;
Message = msg;