Time format is changed to decimal when calling the time data from excel.

8 views (last 30 days)
In matlab, when i read a time data (hh:mm:ss AM/PM) from excel, the time data becomes decimal number so unable to plot the time in x-axis in hh:mm:ss format. Kindly guide

Accepted Answer

Ganesh
Ganesh on 13 Jun 2024
time_in_decimal = readmatrix("TimeData.xlsx");
timeDuration = time_in_decimal * 24 * 60 * 60;
timeDuration = seconds(timeDuration);
sampleY = [1.75 12.2 4.67 9.2 19.1];
plot(timeDuration, sampleY);
xtickformat("hh:mm:ss");
xlabel('Time (hh:mm:ss)');
ylabel('Random Y-axis Label');
You can use the "xtickformat()" option to plot it in the required format.
Hope this helps!

More Answers (2)

Matlab Pro
Matlab Pro on 13 Jun 2024
Another soltion using timeseries funciton
Have fun...
out = readtable('dates1.xlsx');
outFormat = 'hh:mm:ss AM';
vTime = out.time;
ts1 = timeseries(vData,vTime);
ts1.Name = 'Daily Count';
ts1.TimeInfo.Units = 'days';
ts1.TimeInfo.StartDate = '01-Jan-2011'; % Set start date.
ts1.TimeInfo.Format = 'HH:MM:SS AM'; % Set format for display on x-axis.
figure;plot(ts1)

Steven Lord
Steven Lord on 13 Jun 2024
In matlab, when i read a time data (hh:mm:ss AM/PM) from excel,
How are you reading the data from the Microsoft Excel spreadsheet? Are you using the tools described on this documentation page?
If you're reading in multiple spreadsheets that are all formatted the same, I'd consider using the Import Tool (the second Related Topic on the documentation page to which I linked above) to interactively configure how MATLAB imports the data then generate a function file you can use to read in the second, third, fourth, etc. spreadsheets. I know Import Tool allows you to specify that you want to read individual variables in as datetime arrays (or other data types) rather than just as double arrays.
Alternately if you already have your data in MATLAB and don't want to re-import it, you could try turning those double arrays into a datetime array using the 'ConvertFrom' syntax with either 'excel' or 'excel1904' as the dateType input. See the "Convert Excel Date Number to Datetime" example on that documentation page.

Community Treasure Hunt

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

Start Hunting!