When using Readtable, how do I convert column of data from text to date?

12 visualizaciones (últimos 30 días)
I'm a complete newbie. I have been able to download data from an excel file using readtable as my boss requested. However, the 1st column is a date (and is defined as such in excel), but comes through as text. I need this column to be converted from text to date format. I know this is easy, but I have been trying to find the answer all afternoon.
Thanks.

Respuestas (2)

Walter Roberson
Walter Roberson el 24 de Ag. de 2015
YourTable.Date = datetime(YourTable(1));
to create a new table field named Date that had the appropriate content.
Passing a 'Format' option would be a good idea to be sure the dates are interpreted correctly.
I am assuming here that the "text" is something like '2007/05/19 23:14' not something like '731045.83' which would also be a text representation of an Excel date. If what you received was a numeric (non-text) Excel date such as 731045.83 then you should use the datetime() option 'convertfrom', 'excel' as the numbers are not exactly the same as datenum values.
The first link I referenced indicates that depending on what you want to do with the dates, the newer datetime objects might not be your best choice in current implementations, in which case you might want to use datenum() instead of datetime(). (Note: the options for datenum() are different than for datetime())
  3 comentarios
NJBadger
NJBadger el 24 de Ag. de 2015
oops sorry. it was actually...
final.date = datetime(final(1))
the code above was my failed attempt to correct the issue
Walter Roberson
Walter Roberson el 24 de Ag. de 2015
Try
final.date = datetime(final{:,1}, 'format', 'MM/DD/YYYY'); %or DD/MM/YYYY as appropriate
I have not used tables much so I tend to forget the syntax for accessing the contents of columns :(

Iniciar sesión para comentar.


Peter Perkins
Peter Perkins el 24 de Ag. de 2015
NJBadger, two suggestions:
1) If you use the Import Tool rather than readtable, you can read directly to a datetime variable in the table.
2) If the spreadsheet has column headers, and the first one is "Date", then readtable creates a table with a variable called Date, and to convert those strings to a datetime, do this:
final.Date = datetime(final.Date)
Walter's suggestion to use braces will work, but to access one table variable, using dot subscripting is simpler. Braces is good for multiple table variables.
This assumes you're doing this on Windows with Excel installed. If you're using Linux, readtable with return Excel date numbers, not strings, and you'll need to do something like
final.Date = datetime(final.Date,'ConvertFrom','Excel')
Hope this helps.
  1 comentario
Walter Roberson
Walter Roberson el 24 de Ag. de 2015
braces are useful if you did not have column headers and so need to work positionally rather than by column name.

Iniciar sesión para comentar.

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by