How to convert column datetime to datenum?
75 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Louise Wilson
el 9 de Mayo de 2019
Comentada: Louise Wilson
el 9 de Mayo de 2019
HI everyone,
I have a .csv file with a datetime column, in the format 'yyyy/m/d hh:mm:ss', for example: '2019/5/2 10:21:25'.
So, earlier dates in the month will be yyyy/m/d but later this will change to yyyy/mm/dd.
I want to convert these datetimes to datenum, as code I am using from a colleague which relies on datenum as an input.
I tried the following, where data.DateTime is a column in a table of such datetimes.
formatIn = 'yyyy/m/d HH:MM:SS';
datenumber=datenum(data.DateTime,formatIn)
However, I get the error "too many input arguments".
Thank you in advance for your help!
Louise
2 comentarios
Respuesta aceptada
Stephen23
el 9 de Mayo de 2019
Editada: Stephen23
el 9 de Mayo de 2019
It is not clear from your example what class data has: a table or a non-scalar structure or .. ?
In any case, datetime objects do not need a format to be converted to serial date numbers:
DN = datenum(data.DateTime) % If data is a table or a scalar structure
DN = datenum([data.DateTime]) % If data is a non-scalar structure
As the documentation shows, the format argument is only used for date string inputs:
7 comentarios
Walter Roberson
el 9 de Mayo de 2019
If you have a table object with a variable that is datetime objects (not cell array of datetime objects), then MATLAB will handle that by creating a (column) vector out of the datetime objects, and assigning that entire vector as a column in the table. When you create a vector out of datetime objects, the all take on the Format property of the first datetime object in the vector. It is therefore not possible to have a single datetime object column in a table that has two different formats.
Now, you might be reading in the dates as character vectors out of a file, probably by using readtable(). And for that purpose it can be a problem if the character vectors are not all the same format. If you do not get an outright error message, then the ones that are not in the first format might be assigned NaT (Not A Time). readtable() is usually pretty good about examining a few input lines to try to figure out what the format is, but it can have problems. You might need to pass a Format parameter to readtable() or use detectImportOptions (possibly along with zapping the Format that it generates for that column.)
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!