Get duration of timerange

조회 수: 25 (최근 30일)
Poison Idea fan
Poison Idea fan 2023년 3월 13일
댓글: Poison Idea fan 2023년 3월 14일
I have a timetable. I also have a timerange corresponding to data in that timetable. I am having a hard time thinking of how to get the length, or duration, of that timerange.
I feel it should be simple but I cannot think of a compact way to do it.

채택된 답변

Walter Roberson
Walter Roberson 2023년 3월 14일
R1 = timerange('2018-01-01','2018-05-01','quarters')
R1 =
timetable timerange subscript: Select timetable rows with times in: QUARTERS Starting at, including: 01-Jan-2018 00:00:00 Ending at, but excluding: 01-Jul-2018 00:00:00 See Select Times in Timetable.
R1S = struct(R1);
Warning: Calling STRUCT on an object prevents the object from hiding its implementation details and should thus be avoided. Use DISP or DISPLAY to see the visible public details of an object. See 'help struct' for more information.
R1S.first
ans = datetime
01-Jan-2018
R1S.last
ans = datetime
01-Jul-2018
R1S.unitOfTime
ans = 'quarters'
R1S.last - R1S.first
ans = duration
4344:00:00
R2 = timerange(seconds(2),seconds(4),'closed')
R2 =
timetable timerange subscript: Select timetable rows with times in the closed interval: [2 sec, 4 sec] See Select Times in Timetable.
R2S = struct(R2);
Warning: Calling STRUCT on an object prevents the object from hiding its implementation details and should thus be avoided. Use DISP or DISPLAY to see the visible public details of an object. See 'help struct' for more information.
R2S.first
ans = duration
2 sec
R2S.last
ans = duration
4 sec
R2S.unitOfTime
ans = 0×0 empty char array
R2S.last - R2S.first
ans = duration
2 sec
But what does the "length" or "duration" mean of a time range? In the case of a time range constructed from seconds it is fairly clear, but if the time range was constructed from quarter then do you need the "length" to be the number of quarters? If it was constructed from months do you need the length to be the number of months?
You can see in the quarters example that the difference between last and first is a duration, which by default gets expressed by hours minutes seconds. But because the range was constructed in quarters (a calendar duration) then looking at the duration in hours in misleading -- if you were to add that number of hours to a quarter at a different point in the year the end would probably not be the end of a quarter (quarters are not exactly equal length, and cannot be because 365 is not exactly divisible by 4)
You might have noticed the Warning message in the struct() call. timerange() does not expose any public methods or properties that allow you to get at the endpoint information, so you have to hack it.
  댓글 수: 4
Walter Roberson
Walter Roberson 2023년 3월 14일
You can call hours on a duration that you get by subtracting last-first
Poison Idea fan
Poison Idea fan 2023년 3월 14일
Yeah I just realized I don't need to use quarters. Thanks for the tip on turning the warning off.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by