Discrepancy between datenum and datetime?
이전 댓글 표시
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')

채택된 답변
추가 답변 (1개)
Dave B
2021년 10월 21일
0 개 추천
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.
카테고리
도움말 센터 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


