Krishna Santos
Krishna Santos

Reputation: 85

Time column changes into decimal form when imported to MATLAB

I have datasets from an xlxs file in Excel which I have to import in MATLAB. The problem is that whenever I import the data, time would always come out as decimal.

So in here, the data in the xlxs file shows the format of HH:MM:SS:

enter image description here

This is what it looks like in the import-assistant of the MATLAB

enter image description here

I'm about to generate a line graph for it and this is what it looks like:

enter image description here

This is the sample code that I have used in order to generate the plot, wherein I wanted the time to not show up in decimal form.

datestr(Time,'HH:MM:SS');
title ('Particle Measurement for 02-20-2020 @ UPD')
plot(Time,PM10)
xlabel('Time')
ylabel('Particle Measurement')
hold on
plot(Time,PM1)
hold on
plot(Time,PM25)

Upvotes: 0

Views: 226

Answers (1)

Adriaan
Adriaan

Reputation: 18177

Time in MATLAB are datenum() values, which are decimal days since 0 Jan 0000. So the things you see are correct times. You can use datestr(Time,'HH:mm:ss') to get a string for the time, and datetick('x','HH:mm') to get the tick marks on the axis in hours.

The line datestr(Time,'HH:MM:SS'); in your code is superfluous. MATLAB has to take time to evaluate the date string, and then ... nothing. You don't save the output to a variable, nor do you display it (you suppressed that with the semicolon).

Long story short: use datenum() values when plotting, and datetick() to get the axis labels correct.

Upvotes: 2

Related Questions