Bug? Datetime creation produces both NaT and 1-Jan-1970 values.

조회 수: 2 (최근 30일)
Erik
Erik 2016년 3월 18일
편집: Philip Borghesani 2016년 3월 25일
When I run the following code in either R2014b or R2016a I get:
clear d, d(:,2) = datetime(randi(10,5,3))
d =
NaT 03-Feb-0007
01-Jan-1970 10-Mar-0009
01-Jan-1970 04-Sep-0010
01-Jan-1970 02-Mar-0006
01-Jan-1970 03-Sep-0002
and with 3 columns instead of 2:
clear d, d(:,3) = datetime(randi(10,5,3))
d =
NaT 01-Jan-1970 06-Feb-0008
NaT 01-Jan-1970 03-Feb-0008
01-Jan-1970 01-Jan-1970 08-May-0003
01-Jan-1970 01-Jan-1970 03-Oct-0007
01-Jan-1970 01-Jan-1970 06-Apr-0007
and with a column vector of 6 elements instead of 5:
clear d, d(:,3) = datetime(randi(10,6,3))
d =
NaT 01-Jan-1970 08-Jul-0008
NaT 01-Jan-1970 08-Apr-0001
01-Jan-1970 01-Jan-1970 02-Oct-0003
01-Jan-1970 01-Jan-1970 05-Jan-0001
01-Jan-1970 01-Jan-1970 05-May-0001
01-Jan-1970 01-Jan-1970 07-Apr-0009
So, what this does is create a column vector of some random datetime values and assign it to a non-existent matrix in its last column. If you'd do this with a vector of numeric values, all other values in the matrix would become zeroes by default, e.g.:
clear M, M(:,2) = ones(3,1)
M =
0 1
0 1
0 1
As we can see, the values in the datetime matrix that are not set are not zeroes. This cannot be, because a datetime must always obey a format such as dd-MM-yyyy like in the default behaviour. It appears MATLAB tries to fill the rest of the matrix with either
datetime(0,'ConvertFrom','posixtime')
ans =
01-Jan-1970 00:00:00
or NaT (Not-a-Time), the equivalent of the numeric NaN (Not-a-Number).
Why does MATLAB behave like this? Is this a bug, or am I causing it?

채택된 답변

Peter Perkins
Peter Perkins 2016년 3월 21일
Erik, that does appear to be a bug. You are making an indexed assignment to only some elements of a datetime array that does not yet exist. I will make a note to have this looked at. The work-around would be to pre-allocate the array with NaT:
>> clear d, d = NaT(5,2); d(:,2) = datetime(randi(10,5,3))
d =
NaT 09-Oct-0008
NaT 03-Jun-0003
NaT 09-Feb-0006
NaT 03-Feb-0007
NaT 10-Mar-0009
  댓글 수: 2
Erik
Erik 2016년 3월 25일
편집: Erik 2016년 3월 25일
What is MATLAB's original intention with its currently bugged auto-preallocation of datetime arrays: NaT or some ‘zero’ datetime? Numeric matrices get zeroes, not NaN. Should the default datetime be NaT then?
I know it can't be zero, but maybe using NaT creates problems when the datetime matrix is used by other functions?
Thanks for looking into this, I hope to see it fixed in an upcoming release!
Philip Borghesani
Philip Borghesani 2016년 3월 25일
편집: Philip Borghesani 2016년 3월 25일
Defaulting to NaT is well established by simpler use cases that work correctly:
clear d
d(4)=datetime
d =
NaT NaT NaT 25-Mar-2016 14:34:51
Note that empty also works around the problem:
>> clear d, d = datetime.empty(5,0); d(:,2) = datetime(randi(10,5,3))
d =
NaT 03-Oct-0007
NaT 06-Apr-0007
NaT 07-Jun-0002
NaT 09-Mar-0002
NaT 10-Aug-0005

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by