How to display the time by a method other than the digital timer method from "duration" or "datetime"
조회 수: 4 (최근 30일)
이전 댓글 표시
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
채택된 답변
Walter Roberson
2021년 10월 20일
hms() can be called passing in a duration object or a datetime object.
S = seconds(finTime) ;
[h, m, s] = hms(S) ;
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!