Different values of datnumber for same date-time?

2 次查看(过去 30 天)
When I convert given date-number '2011-01-06 0:45' by
datenum(2011-01-06 0:45,'yyyy-mm-dd HH:MM')
I get
734509.0313
as date-number.
But When I generate a vector of date-numbers using
begin_date_time=datenum('2011-01-06 00:00:00','yyyy-mm-dd HH:MM:SS');
str_next_date=strcat('2011-01-06',32,'00:',time_scale{ind_node},':00');
next_date_time=datenum(str_next_date,'yyyy-mm-dd HH:MM:SS');
interval=next_date_time-begin_date_time;
end_date_time=datenum('2011-12-31 00:00:00','yyyy-mm-dd HH:MM:SS');
full_data_time_steps=begin_date_time:interval:end_date_time;
The fourth date-number in the vector 'full_data_time_steps' is
734509.0312.
But it corresponds to the same date-time '2011-01-06 0:45'. I am not sure why this is happening and how to solve this? Any help is appreciated.

采纳的回答

dpb
dpb 2018-8-6
It's the result of floating point rounding in the two different ways you generated the datenum values --
In
full_data_time_steps=begin_date_time:interval:end_date_time;
the interval value is an approximation and over the vector it will accumulate noticeable differences.
The solution is to always use integer values of whatever granularity is needed to generate sequences of dates such that the internal calculations are reproducible.
Presuming you're using ML release R2014b or later use datetime instead of datenum; there are many features that make it more flexible.
  4 个评论
dpb
dpb 2018-8-6
No problem; it's a very common issue and the doc doesn't do enough to explain the problem and how to avoid it.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

标签

产品


版本

R2014a

Community Treasure Hunt

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

Start Hunting!

Translated by