function [data, trig, list] = exclude_outliers(datastack, triggers, max) %this function excludes an entire epoch if that epoch contains a value %above the max or below -max. makes a new trigger variable excluding the %triggers that correspond to the outliers. makes a list of the indices of %excluded outliers. data = datastack; trig = triggers; list = zeros(length(triggers(:,2)),1); m = 1; for i = 1:length(datastack(1,1,:)) for j = 1:length(datastack(:,1,1)) for k = 1:length(datastack(1,:,1)) if abs(data(j,k,i)) > max data(:,:,i) = (data(:,:,i) - data(:,:,i)); end end end end for i = length(datastack(1,1,:)):-1:1 if i <= length(data(1,1,:)) if any(any(data(:,:,i))) == 0 trig(i,:) = []; data(:,:,i) = []; list(m,1) = i; m = m + 1; end end end for i = length(triggers(:,2)):-1:1 if i <= length(list(:,1)) if list(i,1) == 0 list(i,:) = []; end end end end