Importing date and time from an excel file to matlab

16 views (last 30 days)
I have an excel file with data that looks like this
Date Time PM10
5/29/14 6:01 0.148
5/29/14 6:03 0.148
5/29/14 6:05 0.121
5/29/14 6:07 0.123
5/29/14 6:09 0.169
5/29/14 6:11 0.129
5/29/14 6:13 0.124
I want to import it into matlab as table such that mytable.Date would be the first column, and mytable.Time would be second column in the same format as in the excel file and mytable.PM10 would be the third column. I am currently using this code.
PM10data = readtable('PM10 data June 2014 - September 2015.xlsx');
However, the output I get looks like this
'5/29/2014' 0.250775462962963 0.148000000000000
'5/29/2014' 0.252164351851852 0.148000000000000
'5/29/2014' 0.253553240740741 0.121000000000000
'5/29/2014' 0.254942129629630 0.123000000000000
'5/29/2014' 0.256331018518519 0.169000000000000
'5/29/2014' 0.257719907407407 0.129000000000000
'5/29/2014' 0.259108796296296 0.124000000000000
I realize that it converts the time into a datenum but I want it to be a string. How do I fix this? Thank you!

Answers (2)

Martin Schätz
Martin Schätz on 18 Oct 2015
Hi, you can use datestr() function
>> datestr(0.250775462962963)
ans =
6:01 AM
  1 Comment
saadatnk8
saadatnk8 on 22 Oct 2015
Hi Martin, yes the datestr function works for one individual value, but if I do it for the whole array, I get the following error
Subscript indices must either be real positive integers or logicals.
Error in formatdate (line 158)
month = char(strrep(month(dtvector(:,2)), '.', '')); %remove period
Error in dateformverify (line 32)
S = char(formatdate([y,mo,d,h,minute,s],dateformstr,islocal));
Error in datestr (line 194)
S = dateformverify(dtnumber, dateformstr, islocal);
I have further explained my problem in this question, please refer to this http://www.mathworks.com/matlabcentral/answers/250226-timestamp-not-converting-to-a-string

Sign in to comment.


Peter Perkins
Peter Perkins on 22 Oct 2015
If you have R2014b or later, see my response to this very similar question.

Community Treasure Hunt

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

Start Hunting!