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

12 views (last 30 days)
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.

Answers (2)

Walter Roberson
Walter Roberson on 24 Aug 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 Comments
NJBadger
NJBadger on 24 Aug 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 on 24 Aug 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 :(

Sign in to comment.


Peter Perkins
Peter Perkins on 24 Aug 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 Comment
Walter Roberson
Walter Roberson on 24 Aug 2015
braces are useful if you did not have column headers and so need to work positionally rather than by column name.

Sign in to comment.

Categories

Find more on Dates and Time in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!