How to display the time by a method other than the digital timer method from "duration" or "datetime"
5 views (last 30 days)
Show older comments
Hi, I'm Japanese graduated student and use MATLAB in research.I'm not very good at writing in English, so I used Google Translate.
Currently, the time taken for the simulation is measured by the datetime or tic-toc function.The start time / end time of the simulation is acquired by datetime, and the time taken is calculated by the tic-toc function.(I just want to use bothof them.)
For example, it looks like this (the process is different in real):
% getting begining date
startTime=tic;
startDate=datetime;
fprintf('Start at: %s\n',startDate);
% simulation body
hoge=zeros(1,1e7);
for k=1:length(hoge)
hoge(k)=k;
end
% getting finishing date
finTime=toc(startTime);
finDate=datetime;
fprintf('Finish at: %s\n',finDate);
fprintf('Simulation time is %.2f sec\n',finTime);
Here, since the number of seconds is stored in "finTime" by the toc function, the required time is displayed in seconds. However, if it takes more than 1 hour, "39291.24 seconds" will be displayed, and this is confusing.
Then, there was a duration function. If I use this, the required time will be displayed by a digital timer method such as "hh: mm: ss.SS".
duration(0,0,finTime,'format','hh:mm:ss.SSS')
I'm not used to the digital timer format, so I want to display it as "hh hours mm minutes ss.SS seconds" in Japanese.I made the following function by myself, but is there a way to display it only with the functions in MATLAB and each toolbox? There are no restrictions on the use of toolbox.
fprintf('Required time =%s',sec2date(finTime));
function Date = sec2date(sec)
hour=fix(sec/3600);
minutes=fix((sec-hour*3600)/60);
second=sec-hour*3600-minutes*60;
Date = sprintf('%d時間 %d分 %.3f秒',hour,minutes,second);
end
*「時間」,「分」and 「秒」 means "hours", "minutes" and "seconds" in Japanese, individualy.
I appreciate for your kindness.
0 Comments
Accepted Answer
Walter Roberson
on 20 Oct 2021
hms() can be called passing in a duration object or a datetime object.
S = seconds(finTime) ;
[h, m, s] = hms(S) ;
More Answers (0)
See Also
Categories
Find more on Calendar in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!