%% Replace NaN with Moving Median
% Use a moving median to fill missing numeric data.
%
% Create a vector of sample points x and a vector of data A that contains missing values.

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 NaN values in A using a moving median with a window of length 10, and plot both the original data and the filled  data.

F = fillmissing(A,'movmedian',10);  
plot(x,F,'r.-',x,A,'b.-') 
legend('Filled Missing Data','Original Data')

% Copyright 2012 The MathWorks, Inc.