Discrepancy between datenum and datetime?

조회 수: 13 (최근 30일)
Louise Wilson
Louise Wilson 2021년 10월 20일
편집: Louise Wilson 2021년 10월 21일
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')

채택된 답변

Cris LaPierre
Cris LaPierre 2021년 10월 21일
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
Louise Wilson
Louise Wilson 2021년 10월 21일
편집: Louise Wilson 2021년 10월 21일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Dave B
Dave B 2021년 10월 21일
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
Louise Wilson
Louise Wilson 2021년 10월 21일
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!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Calendar에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by