>> help fillmissing
fillmissing Fill missing entries
First argument must be numeric, datetime, duration, calendarDuration,
string, categorical, character array, cell array of character vectors,
a table, or a timetable.
Standard missing data is defined as:
NaN – for double and single floating-point arrays
NaN – for duration and calendarDuration arrays
NaT – for datetime arrays
<missing> – for string arrays
<undefined> – for categorical arrays
blank character [‘ ‘] – for character arrays
empty character {”} – for cell arrays of character vectors
B = fillmissing(A,’constant’,C) fills missing entries in A with the
constant scalar value C. You can also use a vector C to specify
different fill constants for each column (or table variable) in A: C(i)
represents the fill constant used for the i-th column of A. For tables
A, C can also be a cell containing fill constants of different types.
B = fillmissing(A,INTERP) fills standard missing entries using the
interpolation method specified by INTERP, which must be:
‘previous’ – Previous non-missing entry.
‘next’ – Next non-missing entry.
‘nearest’ – Nearest non-missing entry.
‘linear’ – Linear interpolation of non-missing entries.
‘spline’ – Piecewise cubic spline interpolation.
‘pchip’ – Shape-preserving piecewise cubic spline interpolation.
B = fillmissing(A,MOV,K) fills standard missing entries using a
centered moving window formed from neighboring non-missing entries.
K specifies the window length and must be a positive integer scalar.
MOV specifies the moving window method, which must be:
‘movmean’ – Moving average of neighboring non-missing entries.
‘movmedian’ – Moving median of neighboring non-missing entries.
B = fillmissing(A,MOV,[NB NF]) uses a moving window defined by the
previous NB elements, the current element, and the next NF elements.
Optional arguments:
B = fillmissing(A,METHOD,…,’EndValues’,E) also specifies how to
extrapolate leading and trailing missing values. E must be:
‘extrap’ – (default) Use METHOD to also extrapolate missing data.
‘previous’ – Previous non-missing entry.
‘next’ – Next non-missing entry.
‘nearest’ – Nearest non-missing entry.
‘none’ – No extrapolation of missing values.
VALUE – Use an extrapolation constant. VALUE must be a scalar
or a vector of type numeric, duration, or datetime.
B = fillmissing(A,METHOD,…,’SamplePoints’,X) also specifies the
sample points X used by the fill method. X must be a floating-point,
duration, or datetime vector. X must be sorted. X must contain unique
points. You can use X to specify time stamps for the data. By default,
fillmissing uses data sampled uniformly at points X = [1 2 3 … ].
B = fillmissing(A,METHOD,DIM,…) also specifies a dimension DIM to
operate along. A must be an array.
[B,FA] = fillmissing(A,…) also returns a logical array FA indicating
the missing entries of A that were filled. FA has the same size as A.
Arguments supported only for table inputs:
B = fillmissing(A,…,’DataVariables’,DV) fills missing data only in
the table variables specified by DV. The default is all table variables
in A. DV must be a table variable name, a cell array of table variable
names, a vector of table variable indices, a logical vector, or a
function handle that returns a logical scalar (such as @isnumeric).
Output table B has the same size as input table A.
Examples:
% Linear interpolation of NaN entries
a = [NaN 1 2 NaN 4 NaN]
b = fillmissing(a,’linear’)
% Fill leading and trailing NaN entries with their nearest neighbors
a = [NaN 1 2 NaN 4 NaN]
b = fillmissing(a,’linear’,’EndValues’,’nearest’)
% Fill NaN entries with their previous neighbors (zero-order-hold)
A = [1000 1 -10; NaN 1 NaN; NaN 1 NaN; -1 77 5; NaN(1,3)]
B = fillmissing(A,’previous’)
% Fill NaN entries with the mean of each column
A = [NaN(1,3); 13 1 -20; NaN(4,1) (1:4)’ NaN(4,1); -1 7 -10; NaN(1,3)]
C = mean(A,’omitnan’);
B = fillmissing(A,’constant’,C)
% Linear interpolation of NaN entries for non-uniformly spaced data
x = [linspace(-3,1,120) linspace(1.1,7,30)];
a = exp(-0.1*x).*sin(2*x); a(a > -0.2 & a < 0.2) = NaN;
[b,id] = fillmissing(a,’linear’,’SamplePoints’,x);
plot(x,a,’.’, x(id),b(id),’o’)
title(”’linear” fill’)
xlabel(‘Sample points x’);
legend(‘original data’,’filled missing data’)
% Fill missing entries in tables with their previous neighbors
temperature = [21.1 21.5 NaN 23.1 25.7 24.1 25.3 NaN 24.1 25.5]’;
windSpeed = [12.9 13.3 12.1 13.5 10.9 NaN NaN 12.2 10.8 17.1]’;
windDirection = categorical({‘W’ ‘SW’ ‘SW’ ” ‘SW’ ‘S’ …
‘S’ ‘SW’ ‘SW’ ‘SW’})’;
conditions = {‘PTCLDY’ ” ” ‘PTCLDY’ ‘FAIR’ ‘CLEAR’ …
‘CLEAR’ ‘FAIR’ ‘PTCLDY’ ‘MOSUNNY’}’;
T = table(temperature,windSpeed,windDirection,conditions)
U = fillmissing(T,’previous’)
See also ismissing, standardizeMissing, rmmissing, isnan, isnat
filloutliers, smoothdata
Reference page for fillmissing
Other functions named fillmissing