from
mytime.m
by Phillip M. Feldman
displays the current date and time, time since the last invocation, and total elapsed time
|
| mytime(reset) |
function mytime(reset)
%
% When called without the reset argument, mytime() displays both time since
% the last invocation and total elapsed time (since the last reset). If a
% given time value is less than 120 seconds, it is displayed in seconds and
% hundredths of seconds. If at least 2 minutes but less than 120 minutes,
% it is displayed in minutes and hundredths of minutes. If greater than or
% equal to 120 minutes, it is displayed in hours and hundredths of hours.
%
% When called with the reset argument (the value of this argument is
% unimportant), the timer is reset to zero.
%
% Dr. Phillip M. Feldman, 15 Feb, 2009
fprintf('%s ', datestr(now));
persistent t;
if isempty(t) | nargin
% Start timer:
tic();
t= [0 0];
fprintf('Timer initialized.\n');
else
t(2)= t(1);
t(1)= toc();
dt= t(1) - t(2);
if dt >= 7200
str= ['Last step took ' num2str(dt/3600,'%.2f') ' hrs.'];
elseif dt >= 120
str= ['Last step took ' num2str(dt/60,'%.2f') ' min.'];
else
str= ['Last step took ' num2str(dt,'%.2f') ' sec.'];
end
fprintf('%s', str);
if t(1) >= 7200
str= ['; total elapsed= ' num2str(t(1)/3600,'%.2f') ' hrs.'];
elseif t(1) >= 120
str= ['; total elapsed= ' num2str(t(1)/60,'%.2f') ' min.'];
else
str= ['; total elapsed= ' num2str(t(1),'%.2f') ' sec.'];
end
fprintf('%s\n\n', str);
end
|
|
Contact us at files@mathworks.com