%% Detect Outliers with Sliding Window
% 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 locations of the outliers in A relative to the points in t with a window size of 5 hours. Plot the data and detected outliers.

TF = isoutlier(A,'movmedian',hours(5),'SamplePoints',t);
plot(t,A,t(TF),A(TF),'x')
legend('Data','Outlier')

%
% Copyright 2012 The MathWorks, Inc.