table

Methods

Method Reference: 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, 'constant', val) 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.

tblB = fillmissing (tblA, method) fills missing entries using the gap-filling method method, which can be one of:

'previous'
Fill with the previous non-missing entry along each column.
'next'
Fill with the next non-missing entry along each column.
'nearest'
Fill with the nearest non-missing entry along each column. When two non-missing entries are equidistant, the later (next) one is used.
'linear'
Fill numeric variables by linear interpolation of neighboring non-missing entries. Non-numeric variables are left unchanged.

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'
Restrict the operation to the indicated subset of table variables. The value uses the same variable referencing as the rest of the table methods. By default, every variable is targeted.
'EndValues'
Control how the 'linear' method treats leading and trailing missing entries. Valid values are 'extrap' (default, linear extrapolation), 'none' (leave them missing), or a numeric scalar used as a constant for the end gaps.

[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

Example: 1

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

Example: 2

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