Why does adding time directly to datenum not add the anticipated amount of time?
Mostrar comentarios más antiguos
Matlab time is stored as a numerical value in units of days since 00-Jan-0000. Going on this premise, why is it that when I add 1/1400 (the value of one minute as there are 1400 minutes in a day) that occasionally I will lose a second.
For example
mytime = datenum('03-04-2015','mm-dd-yyyy'); % mytime should equal 736027
If I systematically add 1/1400 to this datenum (the value of one minute as there are 1400 minutes in a day), the time will not change minute by minute. This can be shown easily with the following loop.
for i = 1:1440
mytime = mytime + 1/1440;
if ~strcmp(datestr(mytime,'SS'),'00')
disp(i)
disp(datestr(mytime,'dd-mm-yyyy HH:MM:SS')
break
end
end
On the 137th iteration, a full second is dropped. I can effectively add time using the datenum or minutes function, setting it to one minute and adding that, but it takes significantly longer and isn't especially efficient. I'm working with a large amount of data and adding time using this method makes my program run way too slow. Why is this happening and how can I get around it without crippling my runtime?
Respuesta aceptada
Más respuestas (1)
James Tursa
el 24 de Jun. de 2015
Editada: James Tursa
el 24 de Jun. de 2015
I will advise using Guillaume's suggestion (datevec or datetime), but will point out that you are not losing a full 1 sec of accuracy after only 137 iterations. Your printed date & time value is a truncated one. E.g., after running your code for 140 iterations and using a different format that prints out the fraction of seconds I get this:
mytime = datenum('03-04-2015','mm-dd-yyyy'); % mytime should equal 736027
mytime0 = mytime;
for i = 1:1440
mytime = mytime + 1/1440;
if ~strcmp(datestr(mytime,'SS'),'00')
disp(i)
disp(datestr(mytime,'dd-mm-yyyy HH:MM:SS.FFF'))
break
end
end
140
04-03-2015 02:19:59.999
So it is much closer to the '00' value than you think. E.g.,
>> 86400*(mytime-mytime0)
ans =
8399.99949932098
Categorías
Más información sobre Time Series Objects en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!