fillmissing

Replaces missing values (NaN) by interpolation, a constant or a neighbor.

Category: Language Fundamentals > Data Types

Syntax

B = fillmissing(A, 'linear')

Fills gaps by linear interpolation between the nearest valid values.

B = fillmissing(A, 'constant', v)

Replaces missing entries by the value v.

B = fillmissing(A, method)

Other methods: 'previous', 'next', 'nearest'.

[B, TF] = fillmissing(___)

Also returns a logical array marking the filled positions.

Description

A value counts as missing when it is NaN (for numeric data). 'linear' draws a straight line between the closest valid points on each side of a gap, 'previous' repeats the last valid value, 'next' takes the following one and 'nearest' takes whichever is closer.

A gap at the edge of the data has a valid neighbor on one side only, so 'linear' cannot interpolate there.

Input arguments

A
Vector, matrix or table. A matrix is processed column by column.
method
'linear', 'previous', 'next', 'nearest' or 'constant'.
v
Fill value, required with 'constant'.

Output arguments

B
Array of the same size as A with the gaps filled.
TF
Logical array, true where a value was filled.

Example

Filling NaN

x = [1 NaN 3 NaN NaN 6];
fillmissing(x, 'linear')
fillmissing(x, 'previous')
fillmissing(x, 'constant', 0)
Output
ans = 1×6

     1     2     3     4     5     6
ans = 1×6

     1     1     3     3     3     6
ans = 1×6

     1     0     3     0     0     6

See also

ismissing rmmissing standardizeMissing isnan interp1 movmean