MATLAB: How to import correct time format from Excel

time excel format

Hi guys.
I have a rather trivial problem:
I want to read an .xlsx file that contains a column with time, eg. 01:00 02:00 03:00 etc.
When I use this command
[obsdata,txt,raw]=xlsread([obsdir,'\',StationName])
the column of time appears messed up, like 0.0416666666666667 0.0833333333333333 0.125000000000000 0.166666666666667 etc. I know this is a problem originating from the format used in excel. How can I solve it? I would appreciate any help….

Best Answer

It is likely best to use readtable to read the Excel file.
Otherwise, use datetime to convert the vector from Excel to a datetime array:
tv = [0.0416666666666667 0.0833333333333333 0.125000000000000 0.166666666666667].';
times = datetime(tv, 'ConvertFrom','excel', 'Format','HH:mm')
producing:
times =
4×1 datetime array
01:00
02:00
03:00
04:00