When using Readtable, how do I convert column of data from text to date?
12 views (last 30 days)
Show older comments
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.
0 Comments
Answers (2)
Walter Roberson
on 24 Aug 2015
Drawing upon the discussion in http://www.mathworks.com/matlabcentral/answers/223442-table-group-by-datetime and http://www.mathworks.com/matlabcentral/answers/180087-using-the-new-datetime-structure-within-datasets
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
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 :(
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
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.
See Also
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!