Importing date and time from an excel file to matlab

조회 수: 1 (최근 30일)
saadatnk8
saadatnk8 2015년 10월 17일
답변: Peter Perkins 2015년 10월 22일
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!

답변 (2개)

Martin Schätz
Martin Schätz 2015년 10월 18일
Hi, you can use datestr() function
>> datestr(0.250775462962963)
ans =
6:01 AM
  댓글 수: 1
saadatnk8
saadatnk8 2015년 10월 22일
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

댓글을 달려면 로그인하십시오.


Peter Perkins
Peter Perkins 2015년 10월 22일
If you have R2014b or later, see my response to this very similar question.

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by