Datevec to Datetime conversion results in 1 millisecond error?
17 次查看(过去 30 天)
显示 更早的评论
When converting a datevector to a datetime, it appears that I'm losing a millisecond and I can't figure out why. It doesn't appear to be a rounding or formatting issue. Any help on what's going on here would be much appreciated.
Thanks,
Brad
0 个评论
采纳的回答
Peter Perkins
2015-10-28
Screen shots are probably not the most convenient way for others to diagnose this problem.
You haven't lost a millisecond, you never had it to begin with. Here's the deal:
>> dv = [2015 8 13 21 6 19.172; 2015 8 13 21 6 19.180]
dv =
2015 8 13 21 6 19.172
2015 8 13 21 6 19.18
You only think that last value is 19.18. It isn't. It's the floating point value that's closest to 19.18, which happens to be just less than the real number 19.18. The display simply shields you from that. With "numbers", that's probably what you'd want display to do, but in "time", you'd probably be unhappy if the display for something just before midnight rounded up to the next day. So:
>> dt = datetime(2015,8,13,21,6,[19.172 19.180],'Format','dd-MM-yyyy HH:mm:ss.SSSSSS')
dt =
13-08-2015 21:06:19.172000 13-08-2015 21:06:19.179999
Your problem is that by the time you've typed "19.18", you're done for. datetime has the precision to give you what you want, though:
>> dt = datetime('13-08-2015 21:06:19.180','Format','dd-MM-yyyy HH:mm:ss.SSSSSS')
dt =
13-08-2015 21:06:19.180000
>> dt = datetime(2015,8,13,21,6,19,[172 180],'Format','dd-MM-yyyy HH:mm:ss.SSSSSS')
dt =
13-08-2015 21:06:19.172000 13-08-2015 21:06:19.180000
更多回答(1 个)
Ingrid
2015-10-28
I do not seem to be able to reproduce this deviation, so how did you obtain the DV and DT variables?
>> timeVec = [2015 8 13 21 6 19.1800]
timeVec =
1.0e+03 *
Columns 1 through 5
2.015000000000000 0.008000000000000 0.013000000000000 0.021000000000000 0.006000000000000
Column 6
0.019180000000000
>> timeNum = datenum(timeVec)
timeNum =
7.361898793886574e+05
>> timeVecBack = datevec(timeNum)
timeVecBack =
1.0e+03 *
Columns 1 through 4
2.015000000000000 0.008000000000000 0.013000000000000 0.021000000000000
Columns 5 through 6
0.006000000000000 0.019180000000008
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calendar 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!