Convert a sequence of datetimes to seconds from first date

468 次查看(过去 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.

采纳的回答

dpb
dpb 2022-10-15
T=['07-Oct-2022 12:46:50';'07-Oct-2022 12:47:21'];
DT=datetime(T)
DT = 2×1 datetime array
07-Oct-2022 12:46:50 07-Oct-2022 12:47:21
Tsec=seconds(DT-DT(1))
Tsec = 2×1
0 31
Subtracting the first datetime value from the vector turns the datetime into a duration, seconds converts that to the double.
  2 个评论
Mark Brandon
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 CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by