datetime conversion from Excel to MATLAB wrong
Show older comments
Problem:
when read excel into MATLAB, the date that is supposed to be 2012 gets translated into 2008
What I have:
an excel file, which only has one sheet.

What I did:
datetime.setDefaultFormats('default','yyyy/MM/dd hh:mm');
opts = detectImportOptions('MyExcelFile.xlsx');
RC_table = readtable('MyExcelFile.xlsx',opts);
What MATLAB gave me:

What else have I tried:
I tried using different date format in excel and correspondingly in MATLAB. It gives me the same problem.
I also tried in Excel, switch to the generic format. So the first date (2012/8/5 10:00) turns into 39664.4166666667. Then I say something like
t_datetime = datetime(39664.4166666667 + datenum(1900,01,01), 'ConvertFrom', 'datenum')
Somehow MATLAB thinks it is 2008/08/06 10:00 while excel thinks it is 2012/8/5 10:00
I also made sure I was reading the correct excel document.
Accepted Answer
More Answers (1)
Walter Roberson
on 9 Sep 2022
Edited: Walter Roberson
on 9 Sep 2022
datetime.setDefaultFormats('default','yyyy/MM/dd hh:mm');
hh is for 12 hour day. You need HH for 24 hour day.
1 Comment
Tongyao Pu
on 9 Sep 2022
Categories
Find more on Data Import from MATLAB 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!