MATLAB tic-toc results in Minutes format
I am using tic-toc function in my Matlab project as many places. The output time can be 331.5264 or 1234.754 seconds
, etc. Can I output this is minutes format? For eg.开发者_如何转开发 5 minutes and 30.6 seconds
? Thanks!
All you have to do is capture the output from toc
(instead of letting it display its default output), then create an output yourself using the functions fprintf
, floor
, and rem
:
tStart = tic;
% Do some things...
tEnd = toc(tStart);
fprintf('%d minutes and %f seconds\n', floor(tEnd/60), rem(tEnd,60));
While tic and toc do not have any way to display values in minutes, you can process the data slightly before displaying it. Check out the following link to a seconds to hour/minute converter.
Usage would be as follows:
tic
% Do something
time_str = SECS2HMS(toc)
disp(time_str)
I will try this out when I get back on my Windows VM. Hope this helps.
EDIT
You could use the datestr and datenum function built into Matlab as well in the following way. Note that I have not tried this code either, but the link reminded me of the syntax on how to do it (without bringing up Matlab)
tic
%Do something
t=toc;
disp(datestr(datenum(0,0,0,0,0,t),'HH:MM:SS'))
The easiest way I've found is to use:
TIME = tic;
% do calculations which take TIME to complete
fprintf('This took %s', duration([0, 0, toc(TIME)]));
(toc()
returns the time from the stopwatch in seconds, and duration()
expects a three-column matrix expressing a time duration in [hours, minutes, seconds] format).
This has the nice property of keeping most of the timing calculations out of sight.
Hope this helps.
This one suited me:
disp(['Elapsed time is ' num2str(round(toc/60,1)) ' minutes.'])
精彩评论