what is the precision of a MATLAB datenum?
Afficher commentaires plus anciens
I'm trying to timestamp samples at a 20MHz (50 ns period) using datenum, and subsequent timestamps all come out the same when I examine them with datevec (seconds column). Is this just a display issue or am I running up against the precision of datenum?
Réponse acceptée
Plus de réponses (2)
Peter Perkins
le 27 Juil 2015
As Ian says, the precision of datenums for contemporary dates is about 10 microseconds:
>> eps(now)*86400
ans =
1.0058e-05
Since R2014b, you can use the new datetime data type, which has much higher precision than datenum. For example:
>> d0 = datetime(2015,7,27,12,0,0,'Format','dd-MMM-yyyy HH:mm:ss.SSSSSSSSS')
d0 =
27-Jul-2015 12:00:00.000000000
>> d1 = d0 + seconds([1e-3 1e-6 1e-9])
d1 =
27-Jul-2015 12:00:00.001000000 27-Jul-2015 12:00:00.000001000 27-Jul-2015 12:00:00.000000001
>> seconds(d1 - d0)
ans =
0.001 1e-06 1e-09
the cyclist
le 10 Août 2012
Modifié(e) : the cyclist
le 10 Août 2012
The documentation of the datevec() function suggests to me that the precision is 1 millisecond.
A bit of playing around gives that this
>> datenum('01-00-0000 00:00:00.0001')
has a non-zero value, but this
>> datenum('01-00-0000 00:00:00.00001')
is zero, suggesting that maybe it is down around the 100 microseconds, but not 10 microseconds.
1 commentaire
Ian
le 27 Juil 2015
The precision of cyclist's answer is driven by the code converting the date string, not the precision of datenums. Datenums are currently of type double in matlab, with about 15 decimal digits of precision. We're about 736000 days past year 0, so that leaves a precision of about 1e-10 days, or ballpark 10 usecs (as correctly implied by JSullivan's answer):
>> d1=datenum(now);
>> d2=[d1+1e-10, d1+5e-11];
>> d2==d1
ans =
0 1
Catégories
En savoir plus sur Dates and Time dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!