How to display the time by a method other than the digital timer method from "duration" or "datetime"
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
gafakel
am 20 Okt. 2021
Kommentiert: gafakel
am 20 Okt. 2021
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 Kommentare
Akzeptierte Antwort
Walter Roberson
am 20 Okt. 2021
hms() can be called passing in a duration object or a datetime object.
S = seconds(finTime) ;
[h, m, s] = hms(S) ;
1 Kommentar
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Calendar finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!