table.fillmissing
table: tblB = fillmissing (tblA, 'constant', val)
table: tblB = fillmissing (tblA, method)
table: tblB = fillmissing (…, Name, Value)
table: [tblB, TF] = fillmissing (…)
Fill missing entries of a table, variable by variable.
tblB = fillmissing (tblA, replaces the missing entries of each table variable with the
fill value val. val can be a scalar that is broadcast to
every targeted variable, a vector with one element per targeted
variable, or a cell array with one fill value per targeted variable.
The fill value of each variable must be compatible with that variable’s
data type.
'constant',
val)
tblB = fillmissing (tblA, method) fills missing
entries using the gap-filling method method, which can be one of:
'previous''next''nearest''linear'datetime and duration
variables can be interpolated; a targeted variable of any other type
raises an error. The 'previous', 'next', and 'nearest' methods
operate on variables of any data type. Leading or trailing missing
entries that cannot be reached by the method are left missing.
The following Name/Value pairs are supported:
'DataVariables'table
methods. By default, every variable is targeted.'EndValues''extrap' (default), which leaves them to the fill
method itself, 'none', which leaves them missing,
'previous', 'next' and 'nearest', which take
the value of the nearest entry that is not missing on the side they
name and leave the other side missing, or a scalar constant, which must
be assignable to the variable it fills. [tblB, TF] = fillmissing (…) also returns a
logical array TF with height (tblA) rows and one
column per table variable. TF(i,j) is true when an
entry of the j-th variable in the i-th row was missing and has been
filled.
Not yet supported: the 'spline', 'pchip',
'makima', 'movmean', 'movmedian',
'mean', 'median', 'mode', and 'knn'
methods, as well as the 'ReplaceValues', 'MaxGap',
'SamplePoints', and 'MissingLocations' options.
Source Code: table
fillmissing replaces gaps rather than dropping rows. Fill with a constant, applied across the (numeric) variables.
T = table ([38; NaN; 40], [71; 69; NaN], 'VariableNames', {'Age', 'Height'})
T =
3x2 table
Age Height
___ ______
38 71
NaN 69
40 NaN
fillmissing (T, 'constant', 0)
ans =
3x2 table
Age Height
___ ______
38 71
0 69
40 0
Interpolation rules fill from neighbouring rows and work across types — 'previous' carries the last value forward (here through both a datetime and a numeric column), while 'linear' interpolates numeric gaps.
Date = datetime (2024, 1, [1; NaN; 3; NaN]); Reading = [10; NaN; 30; 40]; T = table (Date, Reading)
T =
4x2 table
Date Reading
___________ _______
01-Jan-2024 10
NaT NaN
03-Jan-2024 30
NaT 40
fillmissing (T, 'previous')
ans =
4x2 table
Date Reading
___________ _______
01-Jan-2024 10
01-Jan-2024 10
03-Jan-2024 30
03-Jan-2024 40
fillmissing (table ([1; NaN; NaN; 4], 'VariableNames', {'v'}), 'linear')
ans =
4x1 table
v
_
1
2
3
4