%% Determine Outliers with Sliding Window
% Use a moving median to find local outliers within a sine wave that corresponds to a time vector.
% Create a vector of data containing a local outlier.

x = -2*pi:0.1:2*pi;
A = sin(x);
A(47) = 0;

%
% Create a time vector that corresponds to the data in A.

t = datetime(2017,1,1,0,0,0) + hours(0:length(x)-1);

%
% Define outliers as points more than three local scaled MAD away from the local median within a sliding window. Find the location of the outlier in A relative to the points in t with a window size of 5 hours. Fill the outlier with the computed threshold value using the method 'clip', and plot the original and filled data.

[B,TF,U,L,C] = filloutliers(A,'clip','movmedian',hours(5),'SamplePoints',t);
plot(t,A,t,B,'o')
legend('Original Data','Filled Data')

%
% Display the threshold value that replaced the outlier.

L(TF)

%
% Copyright 2012 The MathWorks, Inc.