7 Mar 2023

Matlab: convert time duration to string

收藏到CSDN网摘

Matlab has its own tic/toc function to measure the running time a peice of code, which is very convenient and widely used by developers. The duration() function along with hours(), minutes(), and seconds() is another way of creating time durations explicitly. All of these duration types need to be converted to string format for displaying purpose. How can/should we convert them properly? The string() function is the answer! To convert a duration object to string, the second input parameter format can be chosen from the list below: dd:hh:mm:ss hh:mm:ss mm:ss hh:mm Note: the first 3 can be used together with '.SSSS' to specify the fractional seconds output, e.g. hh:mm:ss.SSSS.
>> d = hours(1)+minutes(25)+seconds(345)

d = 

  duration

   1.5125 hr

>> string(d,'hh:mm:ss.SSSS')

ans = 

    "01:30:45.0000"

>> string(d,'hh:mm:ss')

ans = 

    "01:30:45"

>> string(d,'mm:ss')

ans = 

    "90:45"

>> string(d,'hh:mm')

ans = 

    "01:30"

No comments :

Post a Comment