Discrepancy between datenum and datetime?

13 views (last 30 days)
I am trying to plot some data (attached) .
The figure wasn't displaying the data I knew I had, so I tried again using datetime instead of datenum for the x-axis. These values are derived from the datenum data but for some reason they display the data correctly, whereas in the datenum plot, some data is missing and there is extra data in January... can anyone explain what is happening here?
sd_dn=datenum(2020,1,1,8,0,0); %date range in datetime
ed_dn=datenum(2020,6,7,17,0,0);
subplot(2,1,1)
title('time as datenum')
hold on
bar(y(:,1),y(:,2))
datetick('x','keepticks'); %change x axis to readable time
xlim([sd_dn, ed_dn]); %filter to date range of interest
subplot(2,1,2)
title('time as datetime')
hold on
y_dt=datetime(y(:,1),'ConvertFrom','datenum');
bar(y_dt,y(:,2))
xlim([(datetime(sd_dn,'ConvertFrom','datenum')), (datetime(ed_dn,'ConvertFrom','datenum'))]);
save('y.mat','y')

Accepted Answer

Cris LaPierre
Cris LaPierre on 21 Oct 2021
It looks to me like there may be rendering issue here when the x values are datenums as opposed to datetimes. I added a scatter plot to show where the bars should be. You can see the second grouping is missing some bars, too.
load yDATENUMS.mat
bar(y(:,1),y(:,2))
hold on
scatter(y(:,1),y(:,2))
hold off
However, if I zoom in, the missing bars appear. If I zoom out, they disappear again.
figure
bar(y(:,1),y(:,2))
% zoom in to the missing side
xlim([737865 737951])
One small additional comment: I don't think the 'keepticks' option in datetick is doing what you think it is doing. I would leave it off. The x axes align better that way (in your orginal figure, Mar appears twice, for example).
sd_dn=datenum(2020,1,1,8,0,0); %date range in datetime
ed_dn=datenum(2020,6,7,17,0,0);
figure
subplot(2,1,1)
title('time as datenum')
bar(y(:,1),y(:,2))
datetick('x'); %change x axis to readable time
xlim([sd_dn, ed_dn]); %filter to date range of interest
subplot(2,1,2)
title('time as datetime')
y_dt=datetime(y(:,1),'ConvertFrom','datenum');
bar(y_dt,y(:,2))
xlim([(datetime(sd_dn,'ConvertFrom','datenum')), (datetime(ed_dn,'ConvertFrom','datenum'))]);
One last comment. These figures are rendering differently here that in desktop MATLAB. Be sure to test this on your computer to see what I am trying to explain.
  1 Comment
Louise Wilson
Louise Wilson on 21 Oct 2021
Edited: Louise Wilson on 21 Oct 2021
Hm, yes, I can see that it is different. I'm glad I noticed. I am in the bad habit of using datenum as I am often adapting code of previous colleagues, but where I can, I will make a more conscientous effort to work with datetime from now on. I can see it makes things a lot easier. Thank you!
I wasn't sure why Mar was appearing twice but assumed it was some sort of datenum error.

Sign in to comment.

More Answers (1)

Dave B
Dave B on 21 Oct 2021
This is one of the many reasons we have datetime, and why we recommend using datetime instead of datenum. If you can use datetime, you should, it works better!
If you really must use datenum, here are a few workaround you can try (these both worked for me with your dataset/snippet). These very subtly change the appearance but maybe that's okay given how zoomed out the view is:
  • Use edges instead of faces: bar(...,'FaceColor','none','EdgeColor','flat')
  • Use a BarWidth of 1: bar(...,'BarWidth',1)
I suspect this is about floating point errors, and MATLAB is seeing the bars as 0 pixels wide and so not rendering them. Supporting this notion: the data aren't invisible with plot (or edges instead of faces), the bars can be seen if you zoom in far enough, and the bars are visible if you subtract y(1,1) from the x values in the datenum case.
  1 Comment
Louise Wilson
Louise Wilson on 21 Oct 2021
Thank you! I inherited code from previous staff at my institution and they use datenum in EVERYTHING. I am guessing this is because datetime is relatively new? I can see now that when I use datetime, everything is better!

Sign in to comment.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!