MATLAB: Error When Using fillmissing Function on Table

datatypefillmissingMATLABnannat;replace

I have a table and I am trying to replace all the NaN values with 0. Based on the documentation for 'fillmissing' this should be straightforward but when I execute the commands: 
d = [NaT; NaT; NaT; NaT; NaT];
x = [2; 4; 3; NaN; NaN];
t = table(d,x)
fillmissing(t,'constant',0)
It produces the following error:
Error using fillmissing/checkArrayType (line 517)
Invalid fill constant type.
Error in fillmissing/fillTableVar (line 155)
[intConstVj,extMethodVj] = checkArrayType(Avj,intMethod,intConstVj,extMethodVj,x,true);
Error in fillmissing/fillTable (line 133)
B.(vj) = fillTableVar(indVj,A.(vj),intMethod,intConst,extMethod,x,useJthFillConstant,useJthExtrapConstant);
Error in fillmissing (line 116)
B = fillTable(A,intM,intC,extM,x,dataVars);
Why do I get this error and how can I prevent this? 

Best Answer

The reason this happens is because 'fillmissing' is not meant to be called on a table with multiple data types using this syntax. There are two data types in table 't' and 'fillmissing' is trying to replace all empty values with 0. Because 0 is not a valid datetime, the error is thrown. You can try one of the following:
1) If only the first column of the table is datetime data and the rest is numeric, run the command:
t(:,2) = fillmissing(t(:,2),'constant',0)
This function call will only work since the rest of the table is of the same datatype: numeric.  
2) Specify which datatype needs to be replaced with the following command:
fillmissing(t,'constant',-1, 'DataVariables', @isnumeric);
In this case, all columns with numeric data will be affected by this command. If the datatimes needed to be replaced, the following command would work: 
fillmissing(t,'constant',datetime('now'), 'DataVariables', @isdatetime);
For tables with multiple datatypes, one 'fillmissing' call must be made for every unique datatype in the table.