Datestr problem with Hour

조회 수: 3 (최근 30일)
Harold Kidder
Harold Kidder 2014년 6월 15일
댓글: dpb 2014년 6월 16일
I wrote a test routine to set a start time stamp and step through hours.
function HourlyDateTEST2()
dvec(1)=2014;
dvec(2)=06;
dvec(3)=15;
dvec(4)=21;
dvec(5)=00;
dvec(6)=00;
dateVal=datenum(dvec);
for ii = 1:7
datestr(dateVal, 'mm/dd/yyyy HH:MM:SS')
dateVal = dateVal + 1/24;
end;
end
The output looks like the following:
06/15/2014 21:00:00
06/15/2014 22:00:00
06/15/2014 23:00:00
06/15/2014 00:00:00
06/16/2014 01:00:00
06/16/2014 02:00:00
06/16/2014 03:00:00
Notice that the 4th line has the wrong date. Should be 06/16/2014 00:00:00.
What am I doing wrong?
I am using MATLAB R2014a.

채택된 답변

Star Strider
Star Strider 2014년 6월 15일
That’s easiest with the addtodate function.
  댓글 수: 13
Star Strider
Star Strider 2014년 6월 16일
I reported this a a bug to TMW yesterday. I got an e-mail a few minutes ago that said that they were able to reproduce it in R2014a, and will fix it ‘in a future release’. Whether this means an update or wait until R2014b I’m not sure. I sent them the link to this thread, so they have all the information they need.
Congratulations to Harold for discovering it!
dpb
dpb 2014년 6월 16일
I'd still be curious if you can generate the two cases to see the actual datenum values. You running 32- or 64-bit version?
I'm still greatly puzzled how can't now seem to reproduce it here when was certain saw it in the original loop version. I suppose I could have mistakenly thought I saw what I didn't.

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

추가 답변 (1개)

dpb
dpb 2014년 6월 15일
What am I doing wrong?
for ii = 1:7
datestr(dateVal, 'mm/dd/yyyy HH:MM:SS')
dateVal = dateVal + 1/24;
...
Using floating point arithmetic; the rounding in the accumulation of the 1/24 factor caused it.
Use
>> datestr(datenum(dvec(1),dvec(2),dvec(3),dvec(4)+[0:7].',dvec(5),dvec(6)))
ans =
15-Jun-2014 21:00:00
15-Jun-2014 22:00:00
15-Jun-2014 23:00:00
16-Jun-2014 00:00:00
16-Jun-2014 01:00:00
16-Jun-2014 02:00:00
16-Jun-2014 03:00:00
16-Jun-2014 04:00:00
>>
instead (add the integer hours, not the fractional days) to avoid rounding errors.

카테고리

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