From: radar on
I don't see why the output of this code should change as shown below.

clc

dv_cur = [ 2010 5 12 7 30 0];
dt_cur = datenum(dv_cur);

dv_end = [ 2010 5 12 15 15 0];
dt_end = datenum(dv_end);

while (dt_cur < dt_end)

fprintf('current datetime is %s\n', datestr(dt_cur));
dt_cur = addtodate(dt_cur, 1, 'minute');

end;

current datetime is 12-May-2010 07:31:00
current datetime is 12-May-2010 07:32:00
current datetime is 12-May-2010 07:33:00
current datetime is 12-May-2010 07:34:00

to

current datetime is 12-May-2010 10:04:00
current datetime is 12-May-2010 10:04:59
current datetime is 12-May-2010 10:05:59
current datetime is 12-May-2010 10:06:59
current datetime is 12-May-2010 10:07:59
current datetime is 12-May-2010 10:08:59
current datetime is 12-May-2010 10:09:59
current datetime is 12-May-2010 10:10:59
From: Steven Lord on

"radar" <able(a)tds.net> wrote in message
news:926292603.124109.1273679295245.JavaMail.root(a)gallium.mathforum.org...
>I don't see why the output of this code should change as shown below.

Since a change of 1 in a serial date number represents the passage of a day,
(1/86400) would represent the passage of a second and (1/1440) would
represent the passage of a minute. However, neither of those numbers can be
exactly represented in double precision, so:


second = (1/86400);
z = 0;
for k = 1:86400
z = z+second;
end
(z-1) % in exact arithmetic, would be 0
minute = (1/1440);
z2 = 0;
for k = 1:1440
z2 = z2+minute;
end
(z2-1) % in exact arithmetic, would be 0


Neither z1 nor z2 are exactly 1, are they? This behavior is NOT, repeat NOT
a bug.

See Q6.1 in the newsgroup FAQ and in particular the bottom of the first
column of page 2 of the Cleve's Corner article linked in that question.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


From: radar on
Thanks for the detailed response.
From: radar on
since my method of using addtodate will not reliably create 1 minute intervals, how else can I do this?

using colon notation with time doesn't work for obvious reasons.

I need every one minute interval as a datestr or datenum from 7:30 to 3:15.
From: TideMan on
On May 13, 1:57 pm, radar <a...(a)tds.net> wrote:
> since my method of using addtodate will not reliably create 1 minute intervals, how else can I do this?
>
> using colon notation with time doesn't work for obvious reasons.
>
> I need every one minute interval as a datestr or datenum from 7:30 to 3:15.

Instead of converting to Matlab days, then incrementing, increment
then convert to Matlab days:

Say, you want to generate nt=100 times from yr, mon, day,hr,mn,sec
onwards:
% Generate a matrix of times
tmx=ones(nt,1)*[yr mon day hr mn sec];
% Replace the minutes column
tmx(:,5)=mn + [0:nt-1]'*15;
% Convert to Matlab days
datenum(tmx)

This works because datenum is very smart.
It knows the different number bases of the individual components
(months, days, etc), and if you enter a number that is bigger than the
number basis (e.g., 26 hours), it cleverly converts that to 1 day 2
hours.

And because you do most of the work in integers, the floating point
problem does not arise.