dat_2011=(datenum(2011,01,01,0,0,0):1/24:datenum(2011,12,31,23,0,0))';
... Did I maybe do something wrong when I created the vector dat_2011?
Ayup...you generated the date vector from the two end date values and the floating point delta using colon instead of using internally (to datenum) generated values. Use
dat_2011=(datenum(2011,1,1, [0:24*365-1].',0,0);
instead. Internally datenum will generate self-consistent values that will work for comparisons while the colon operation uses different algorithms to minimize error between initial and final values.
The general rule is to use integer-valued increments of the proper size to span the desired time and granularity desired rather than using the fractional days in floating point and introducing that external rounding error. The values will be very similar but when you use floating point comparisons later, even a single bit in the least significant position will cause a failure.
Try looking at the difference between the two series as generated above; I would expect you'll find that the difference is otoh E-15 and will be symmetric around the midpoint of the series owing to how : works internally.
ADDENDUM
I did the comparison..the actual difference in the two is
>> dat_2011=(datenum(2011,01,01,0,0,0):1/24:datenum(2011,12,31,23,0,0))';
>> dn=datenum(2011,1,1,[0:24*365-1].',0,0);
>> max(dat_2011-dn)
ans =
1.1642e-10
>>
In the previous error estimate I was forgetting to factor in the magnitude of datenums being on order of 10E5.
>> eps(dn(1))
ans =
1.1642e-10
>> eps(1)
ans =
2.2204e-16
>>
ADDENDUM 2
...The general rule is to use integer-valued increments of the proper size to span the desired time and granularity desired rather than using the fractional days in floating point and introducing that external rounding error. The values will be very similar but when you use floating point comparisons later, even a single bit in the least significant position will cause a failure.
ATTN: TMW The above caution or similar should be in the documentation on date numbers. This issue arises repeatedly owing to what seems to be a reasonable way to generate the date vector is, in fact, guaranteed to fail as demonstrated above. This question comes up over and over and is, afaik, never mentioned in the doc's. Although one with some experience can infer it from floating point behavior, it'll catch virtually everybody at some time or the other until they've seen/experienced it.