Rounding issues when creating date vectors

I want to create a vector containing dates in matlab. For this, I indicated the start time and stop time:

WHM01_start = datenum('01-JAN-2005 00:00')
WHM01_stop = datenum('01-SEP-2014 00:00')

and then I created a vector with

WHM01_timevec = WHM01_start:datenum('01-JAN-2014 00:20') - datenum('01-JAN-2014 00:00'):WHM01_stop;

after I want to have time steps of 20 minutes each. Unfortunately, I received a rounding error after several thousand values, which led me to

>> datestr(WHM01_timevec(254160))

ans =

31-Aug-2014 23:39:59

and not as expected, 31-Aug-2014 23:40:00
How can I fix these incorrect values?

Edit: I also saw this stream , but, unfortunately, I get there a vector for the date, and not a number as desired.

+4
source share
2 answers

, , ,... datenum. Datenum , (, 120 ), datenum , .

, 20- , ( , 1- ):

WHM01_start = datenum('01-JAN-2005 00:00');
WHM01_stop = datenum('01-SEP-2014 00:00');

time_diff = WHM01_stop - WHM01_start;

WHM01_timevec = test = datenum(2005,01,01,00,[00:20:time_diff*24*60],00);

datestr(WHM01_timevec(254160))

:

, , , . () .

Matlab () 0.0.0000. 1/3 1/(24 * 3) . ,

WHM01_timevec = WHM01_start:1/(24*3):WHM01_stop;

, .

+4

: linspace :.

%// given
WHM01_start = datenum('01-JAN-2005 00:00')
WHM01_stop = datenum('01-SEP-2014 00:00')

%// number of elements
n = numel(WHM01_start: datenum('01-JAN-2014 00:20') - ...
                       datenum('01-JAN-2014 00:00') : WHM01_stop);

%// creating vector using linspace
WHM01_timevec = linspace(WHM01_start, WHM01_stop, n);

%// proof
datestr(WHM01_timevec(254160))

ans =

31-Aug-2014 23:40:00

: , :, , , .


:

linspace , .

+4

All Articles