Convert datetime to numeric - preserve date format
33 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Elin Jacobs
el 27 de Feb. de 2023
Comentada: Campion Loong
el 29 de Mzo. de 2023
I have a timetable with dates (datetime data type) in the format 'yyyy-MM-dd HH:mm'. I want to convert these to a numeric data type (double) but keeping the exact numbers of each date such that the datetime '2023-02-27 14:00' is converted to the double '202302271400'. The function yyyymmdd() allows me to do this for the date, but not the hour and minute, and none of the other built in functions to convert to datenumber seem to have this option. Thanks for any insights to this problem.
0 comentarios
Respuesta aceptada
Les Beckham
el 27 de Feb. de 2023
Editada: Les Beckham
el 28 de Feb. de 2023
Edited to work with datetime array vs a single datetime.
d = datetime(['2023-02-27 14:00'; '2023-02-27 15:00']) % test data - replace with your timetable
s = string(d, 'yyyyMMddHHmm')
format long
% f = cellfun(@(s)sscanf(s, '%f'), s) % original method of converting to double
f = double(s) % An easier/cleaner method. Thanks to Walter for reminding me about this.
compose('%.0f', f)
10 comentarios
Campion Loong
el 29 de Mzo. de 2023
@Elin Jacobs Thanks for confirming. That (unfortunately) makes sense.
Más respuestas (0)
Ver también
Categorías
Más información sobre Data Type Conversion 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!