what is the precision of a MATLAB datenum?

21 次查看(过去 30 天)
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?

采纳的回答

Jonathan Sullivan
Jonathan Sullivan 2012-8-10
Well, let's find out.
Let's take a look at the precision of the time "now," converted to seconds
reference = now;
precision = eps(reference);
precision_seconds = precision*24*3600
Looks like about 10 us for an absolute timestamp.
If you want a relative timestamp with a max value of 1 day, we get the following:
reference = 1;
precision = eps(reference);
precision_seconds = precision*24*3600
Now we get a precision of about 0.02 ns.

更多回答(2 个)

Peter Perkins
Peter Perkins 2015-7-27
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
the cyclist 2012-8-10
编辑:the cyclist 2012-8-10
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 个评论
Ian
Ian 2015-7-27
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by