Convert a sequence of datetimes to seconds from first date
조회 수: 440 (최근 30일)
이전 댓글 표시
I have long been dealing with data in which I have a vector of measured physical values and a vector of datetimes each of which correspond to the datetime at which the physical values were reported by instruments. I routinely need to evaluate the data against the time in seconds from the first elements, i.e seconds from the time of the first measurement. For example, I will have an array of date times the first two elements of which might be ['07-Oct-2022 12:46:50' '07-Oct-2022 12:47:21'] and an array of physical measurements the first two elements of which might be [5.22347300000000 3.86309200000000']. For many years I have been quite content converting the datetimes to date numbers using datenum and multiplying the result by 24*3600. For example if the date time array above is named DT then I have been content calculating the time in seconds, say Tsec, by Tsec = (datenum(DT) - datenum(DT(end))*24*3600, and getting a vector of numeric double seconds starting with zero and ending with the number of seconds between the first datetime entry and each other entry in the date time array.
I just updated to R2022b and it says datenum is not recommended and to use other things involving calendardurations and durations and datetime only. I have struggled a good bit trying to use those other options to get a simple sequence of double precision numbers that represent the seconds from the first datetime, so far without success. I am sure the answer must be simple since this is an obvious need. The datetimes in the orginal data is needed because different instruments may measure different physical variables and each instrument may have different start times and differnt sampling intervals but all the instruments need to be synchronized during post processing.
So the simple form of the question is what should I use instead of Tsec = (datenum(DT) - datenum(DT(1))*24*3600 ?
I have gotten lots of error messages in the variations I have tried, such as (following in red)
Error using duration
Input data must be numeric, or a cell or string array containing duration text.
Or
Error using seconds
Input data must be a real, numeric array, or a duration array. Use TIME or SPLIT to extract durations from a calendarDuration array.
Etc.
Thanks much.
댓글 수: 0
채택된 답변
dpb
2022년 10월 15일
T=['07-Oct-2022 12:46:50';'07-Oct-2022 12:47:21'];
DT=datetime(T)
Tsec=seconds(DT-DT(1))
Subtracting the first datetime value from the vector turns the datetime into a duration, seconds converts that to the double.
댓글 수: 2
Mark Brandon
2023년 10월 14일
Please include this example in the Help Center. Probably best to have it as one of the first examples under "seconds". Like Frank, I spent too long trying to sort out issues between the datenum (not recommended) and seconds.
추가 답변 (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!