Convert a sequence of datetimes to seconds from first date
470 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Frank
el 15 de Oct. de 2022
Comentada: Mark Brandon
el 14 de Oct. de 2023
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 comentarios
Respuesta aceptada
dpb
el 15 de Oct. de 2022
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 comentarios
Mark Brandon
el 14 de Oct. de 2023
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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Dates and Time en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!