Can one extract a unit of time from a duration object?

조회 수: 20 (최근 30일)
Alexandre Aksenov
Alexandre Aksenov 2022년 11월 1일
편집: Alexandre Aksenov 2022년 12월 26일
Some of my programs of data analysis produce 'duration' objects, and I am looking for a way of printing them as strings.
I have the option of guessing an appropriate unit of time from data, then calling a (relatively unnatural) instruction like:
sprintf("%.2f sec",seconds(T2print))
Relatively unexpectedly, both "char" and "string" operators' results seem to depend on how the object has been initially created, as the following code shows.
Tsec = seconds(1)
%-> duration
% 1 sec
Tmin = minutes(1)
%-> duration
% "1 min"
isequal(60*Tsec,Tmin)
%true !
string(60*Tsec)
% "60 sec"
char(60*Tsec)
%'60 sec'
string(Tmin)
%"1 min"
char(Tmin)
%'1 min'
It is certainly possible to parse the output of "string", then to extract the unit of time, but I would like to find a simpler way of finding it. Getting identical results for equal durations would obviously be a positive point.
My question is related to this one:
Thanks for your attention!

채택된 답변

Steven Lord
Steven Lord 2022년 11월 1일
MATLAB will try to select a good format for the duration object based on the data with which it was created. But if you want all your duration objects to have a consistent format, you can specify it.
s = seconds(60)
s = duration
60 sec
formatForSeconds = s.Format
formatForSeconds = 's'
m = minutes(1)
m = duration
1 min
formatForMinutes = m.Format
formatForMinutes = 'm'
Let's change the format for m to be the same as the format for s.
m.Format = formatForSeconds
m = duration
60 sec
Now that they're using the same format, converting them to text data will give the same text data.
sString = string(s)
sString = "60 sec"
mString = string(m)
mString = "60 sec"
isequal(sString, mString)
ans = logical
1
See the section about the Format property of duration objects on the duration documentation page for a list of available format components.
  댓글 수: 1
Alexandre Aksenov
Alexandre Aksenov 2022년 12월 26일
편집: Alexandre Aksenov 2022년 12월 26일
Thanks for pointing to the Format property!
This is the exact tool, which is necessary for my needs. Indeed, the property 'Format' allows to build a general instruction, specifying the number of significative digits, and using the appropriate unit of time for any given duration.
On the contrary, the example instruction, which I tried (see the original question) does not answer the problem, precisely because it tries to express any duration in seconds.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Lei Hou
Lei Hou 2022년 11월 18일
Hi Alexandre,
Applying seconds/minutes/... to a duration array returns numeric value
>> d1 = seconds(60)
d1 =
duration
60 sec
>> d2 = minutes(1)
d2 =
duration
1 min
>> x1 = seconds(d1)
x1 =
60
>> x2 = seconds(d2)
x2 =
60
>> isequal(seconds(d1),seconds(d2))
ans =
logical
1
Hoping this is helpful.
Thanks,
Lei
  댓글 수: 1
Alexandre Aksenov
Alexandre Aksenov 2022년 12월 26일
Thanks for your attention!
I am currently using the function 'seconds' to convert any statistic of my data befire printing. This works precisely as you suggest! This approach is sufficient when it has been checked in advance, that the data is of the order of magnitude of seconds.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Time Series Collections에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by