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

bugdatetimeMATLABpreallocationr2014br2016b

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?

Best Answer

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