Filter löschen
Filter löschen

How to display the time by a method other than the digital timer method from "duration" or "datetime"

3 Ansichten (letzte 30 Tage)
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);
Start at: 20-Oct-2021 04:37:57
% 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);
Finish at: 20-Oct-2021 04:37:57
fprintf('Simulation time is %.2f sec\n',finTime);
Simulation time is 0.20 sec
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')
ans = duration
00:00:00.19
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));
Required time =0時間 0分 0.199秒
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.

Akzeptierte Antwort

Walter Roberson
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
gafakel
gafakel am 20 Okt. 2021
That's it ! I really appriciate for your help.
Based on your answer, I will modify my code as follows:
% getting begining date
startTime=tic;
startDate=datetime;
fprintf('Start at: %s\n',startDate);
Start at: 20-Oct-2021 05:43:43
% 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);
Finish at: 20-Oct-2021 05:43:43
fprintf('Simulation time is %.2f sec\n',finTime);
Simulation time is 0.10 sec
%%%%%% New code %%%%%
NOVELfinTime=cell(1,3);
[NOVELfinTime{:}]=hms(seconds(finTime));
NOVELfinTime=cell2mat(NOVELfinTime);
fprintf('%d時間 %d分 %.2f秒',NOVELfinTime);
0時間 0分 0.10秒

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Dates and Time finden Sie in Help Center und File Exchange

Produkte


Version

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by