Datevec to Datetime conversion results in 1 millisecond error?

조회 수: 9 (최근 30일)
Brad
Brad 2015년 10월 28일
댓글: Brad 2015년 10월 28일
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

채택된 답변

Peter Perkins
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
Brad
Brad 2015년 10월 28일
Excellent! So it WAS a floating point / rounding / display issue. Great example process. Very clear and easy to follow.
You're right, the screenshot is difficult to see and work with. I will post inline code for future questions and answers.
Many thanks,
Brad

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ingrid
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
  댓글 수: 1
Brad
Brad 2015년 10월 28일
편집: Brad 2015년 10월 28일
Hi Ingrid,
I'm using the 'datetime' data type, not serial date numbers as you have done with 'datenum()'.
The DV (as in "datevector") and DT (as in "datetime) variables were obtained from timestamp metadata from TIFF images. I converted this serial string data to a datevector, and then to a datetime to be able to easily correct and fix timestamp issues.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Time Series Objects에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by