Syntax Show
Descriptionexample
Missing values are defined according to the data type of
If example
example
example
example
example
example
Examplescollapse all Vector with NaN ValuesCreate
a vector that contains A = [1 3 NaN 4 NaN NaN 5];
F = fillmissing(A,'previous') Matrix with NaN ValuesCreate a 2-by-2 matrix with a F = fillmissing(A,'constant',[100 1000]) Interpolate Missing DataUse interpolation to replace Define a vector of nonuniform sample points and evaluate the sine function over the points. x = [-4*pi:0.1:0, 0.1:0.2:4*pi]; A = sin(x); Inject A(A < 0.75 & A > 0.5) = NaN; Fill the missing data using linear interpolation, and return the filled vector [F,TF] = fillmissing(A,'linear','SamplePoints',x); Plot the original data and filled data. scatter(x,A) hold on scatter(x(TF),F(TF)) legend('Original Data','Filled Data') Use Moving Median MethodUse a moving median to fill missing numeric data. Create a vector of sample points x = linspace(0,10,200); A = sin(x) + 0.5*(rand(size(x))-0.5); A([1:10 randi([1 length(x)],1,50)]) = NaN; Replace F = fillmissing(A,'movmedian',10); plot(x,F,'.-') hold on plot(x,A,'.-') legend('Original Data','Filled Data') Use Custom Fill MethodDefine a
custom function to fill Define a vector of sample points t = 10:10:100; A = [0.1 0.2 0.3 NaN NaN 0.6 0.7 NaN 0.9 1]; scatter(t,A) Use the local function
n = 2;
gapwindow = [10 0];
[F,TF] = fillmissing(A,@(xs,ts,tq) forwardfill(xs,ts,tq,n),gapwindow,'SamplePoints',t); The
gap window value
The function handle input values for the second gap are:
Plot the original data and the filled data. scatter(t,A)
hold on
scatter(t(TF),F(TF)) function y = forwardfill(xs,ts,tq,n) % Fill n values in the missing gap using the previous nonmissing value y = NaN(1,numel(tq)); y(1:min(numel(tq),n)) = xs; end Matrix with Missing EndpointsCreate a matrix with missing entries and fill across the columns (second dimension) one row at a time using linear interpolation. For each row, fill leading and trailing missing values with the nearest nonmissing value in that row. A = [NaN NaN 5 3 NaN 5 7 NaN 9 NaN; 8 9 NaN 1 4 5 NaN 5 NaN 5; NaN 4 9 8 7 2 4 1 1 NaN] A = 3×10
NaN NaN 5 3 NaN 5 7 NaN 9 NaN
8 9 NaN 1 4 5 NaN 5 NaN 5
NaN 4 9 8 7 2 4 1 1 NaN
F = fillmissing(A,'linear',2,'EndValues','nearest') F = 3×10
5 5 5 3 4 5 7 8 9 9
8 9 5 1 4 5 5 5 5 5
4 4 9 8 7 2 4 1 1 1
Table with Multiple Data TypesFill missing values for table variables with different data types. Create a table whose variables include A = table(categorical({'Sunny'; 'Cloudy'; ''}),[66; NaN; 54],{''; 'N'; 'Y'},[37; 39; NaN],... 'VariableNames',{'Description' 'Temperature' 'Rain' 'Humidity'}) A=3×4 table
Description Temperature Rain Humidity
___________ ___________ __________ ________
Sunny 66 {0x0 char} 37
Cloudy NaN {'N' } 39
<undefined> 54 {'Y' } NaN
Replace all missing entries with the value from the previous entry. Since there is no previous element in the F = fillmissing(A,'previous')
F=3×4 table
Description Temperature Rain Humidity
___________ ___________ __________ ________
Sunny 66 {0x0 char} 37
Cloudy 66 {'N' } 39
Cloudy 54 {'Y' } 39
Replace the F = fillmissing(A,'constant',0,'DataVariables',{'Temperature','Humidity'}) F=3×4 table
Description Temperature Rain Humidity
___________ ___________ __________ ________
Sunny 66 {0x0 char} 37
Cloudy 0 {'N' } 39
<undefined> 54 {'Y' } 0
Alternatively, use the F = fillmissing(A,'constant',0,'DataVariables',@isnumeric) F=3×4 table
Description Temperature Rain Humidity
___________ ___________ __________ ________
Sunny 66 {0x0 char} 37
Cloudy 0 {'N' } 39
<undefined> 54 {'Y' } 0
Now fill the missing values in F = fillmissing(A,'constant',{categorical({'None'}),1000,'Unknown',1000}) F=3×4 table
Description Temperature Rain Humidity
___________ ___________ ___________ ________
Sunny 66 {'Unknown'} 37
Cloudy 1000 {'N' } 39
None 54 {'Y' } 1000
Specify Maximum GapCreate a time vector t = seconds([2 4 8 17 98 134 256 311 1001]); A = [1 3 23 NaN NaN NaN 100 NaN 233]; Fill only missing values in F = fillmissing(A,'linear','SamplePoints',t,'MaxGap',seconds(250)) F = 1×9
1.0000 3.0000 23.0000 25.7944 50.9435 62.1210 100.0000 NaN 233.0000
Input Argumentscollapse all A — Input data vector | matrix | multidimensional array | cell array of character vectors | table | timetableInput data, specified as a vector, matrix, multidimensional array, cell array of character vectors, table, or timetable.
v — Fill constant scalar | vector | cell arrayFill constant, specified as a scalar, vector, or cell array.
method — Fill method 'previous' | 'next' | 'nearest' | 'linear' | 'spline' | 'pchip' | 'makima'Fill method, specified as one of these values:
movmethod — Moving method 'movmean' | 'movmedian'Moving method to fill missing data, specified as one of these values:
window — Window length positive integer scalar | two-element vector of positive integers | positive duration scalar | two-element vector of positive durationsWindow length for moving methods, specified as a positive integer scalar, a two-element vector of positive integers, a positive duration scalar, or a two-element vector of positive durations. The window is defined relative to the sample points. If If If fillfun — Custom fill method function handleExample: Custom fill method, specified as a function handle. Valid function handles must include the following three input arguments:
The function must return either a scalar or a vector with the same length as gapwindow — Gap window length positive integer scalar | two-element vector of positive integers | positive duration scalar | two-element vector of positive durationsGap window length for custom fill functions, specified as a positive integer scalar, a two-element vector of positive integers, a positive duration scalar, or a two-element vector of positive durations. The gap window is defined relative to the sample points. When specifying a function handle When dim — Operating dimension positive integer scalarOperating dimension, specified as a positive integer scalar. If no value is specified, then the default is the first array dimension whose size does not equal 1. Consider an
For table or timetable input data, Name-Value ArgumentsSpecify optional pairs of arguments as Before R2021a, use commas to separate each name and value, and enclose Example: Data Options collapse all SamplePoints — Sample points vector | table variable name | scalar | function handle | table vartype subscriptSample points, specified as a vector of sample point values or one of the options in the following table when the input data is a table. The sample points represent the x-axis locations of the data, and must be sorted and contain unique elements. Sample points do not need to be uniformly sampled. The vector When the input data is a table, you can specify the sample points as a table variable using one of these options:
Note This name-value argument is not supported when the input data is a Moving windows are defined relative to the sample points. For example, if When the sample points vector has data type Example: Example: Data Types: DataVariables — Table variables to operate on table variable name | scalar | vector | cell array | function handle | table vartype subscriptTable variables to operate on, specified as one of the options in this
table. The Other variables in the table not specified by
Example: ReplaceValues — Replace values indicator true or 1 (default) | false or 0Replace values indicator, specified as one of these values when
For vector, matrix, or multidimensional array input data, Example: Missing Value Options collapse all EndValues — Method for handling endpoints 'extrap' (default) | 'previous' | 'next' | 'nearest' | 'none' | scalarMethod for handling endpoints, specified as
MissingLocations — Known missing indicator vector | matrix | multidimensional arrayKnown missing indicator, specified as a logical vector, matrix, or multidimensional array of the same size as Data Types: MaxGap — Maximum gap size to fill numeric scalar | duration scalar | calendarDuration scalarMaximum gap size to fill, specified as a numeric scalar, For example, consider the vector For missing values at the beginning or end of the data:
Output Argumentscollapse all F — Filled data vector | matrix | multidimensional array | table | timetableFilled data, returned as a vector, matrix, multidimensional array, table, or timetable.
TF — Filled data indicator vector | matrix | multidimensional arrayFilled data indicator, returned as a vector, matrix, or multidimensional array.
Data Types: Tips
Extended CapabilitiesTall Arrays Calculate with arrays that have more rows than fit in memory.Usage notes and limitations:
For more information, see Tall Arrays. C/C++ Code Generation Generate C and C++ code using MATLAB® Coder™.Usage notes and limitations:
Thread-Based Environment Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool.This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment. Version HistoryIntroduced in R2016b expand all R2022b: Character arrays have no standard missing valueCharacter arrays have no default definition of a standard missing value. Therefore, To treat blank character array elements as missing, use the R2022a: Append filled valuesFor table or timetable input data, append the input table with all table variables that were checked for missing entries. The missing entries
in the appended variables are filled. Append, rather than replace, table variables by setting the R2021b: Specify sample points as table variableFor table input data, specify the sample points as a table variable using the R2021a: Specify custom fill methodFill missing values using a custom method by specifying |