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

5 views (last 30 days)
gafakel on 20 Oct 2021
Commented: gafakel on 20 Oct 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);
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.
0 CommentsShowHide -1 older comments

Sign in to comment.

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) ;
1 CommentShowHide None
gafakel on 20 Oct 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秒

Sign in to comment.

Categories

Find more on Dates and Time in Help Center and File Exchange

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by