importdata not reading time stamp from excel sheet accurately
1 view (last 30 days)
Show older comments
Danya Pradeep Kumar
on 28 Sep 2019
Commented: Ajay Kumar
on 29 Sep 2019
I have been using importdata to read an excel sheet with time stamps. The time stamp on excel sheet is 1/1/2015 12:20:18 AM. When importdata is used, this time stamp gets converted to [42005.014097222]. I tried using datestr([42005.014097222],31) command and this is being converted to '0115-01-02 00:20:18'; but it has to be 2015-01-01 00:20:18'. How can this be fixed? Thank you!
Here is the program:
[filename, pathname] = uigetfile({'*.*'},'Choose the input file');
delimiterIn = ',';
headerlinesIn = 0;
A = importdata([pathname filename],delimiterIn,headerlinesIn); %this is a long array of time stamps, just citing one in the example above.
start_time=A(:,1); %start_time(1)=[42005.014097222]; but the 1st element in the excel sheet is 1/1/2015 12:20:18 AM
stop_time=A(:,2);
[y1,m1,d1,h1,m1,s1]=datevec(datestr(start_time,31)); %the result here gives me y1=115 and d1=2; but the right result should be y1=2015 and d1=1.
[y2,m2,d2,h2,m2,s2]=datevec(datestr(stop_time,31));
0 Comments
Accepted Answer
Ajay Kumar
on 28 Sep 2019
Edited: Ajay Kumar
on 29 Sep 2019
The default date for excel is 1899/12/30_00:00:00. So you have to add this to the date number to get the correct date.
Try this
t1 = datenum('1899/12/30_00:00:00','yyyy/mm/dd_HH:MM:SS');
datestr(t1+42005.014097222)
Please accept the answer if you got what you need. Thanks :)
5 Comments
More Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!