function [data, trig, list, count] = exclude_outliers_std(datastack, triggers, s) %this function excludes an entire epoch if that epoch contains a value %which is s standard deviations above or below the overall mean of the %datastack. then makes a new trigger variable excluding the %triggers that correspond to the outliers and makes a list of the indices of %excluded outliers. the count vector is a count of how many values were %outside the range in each epoch. Mean = mean(mean((mean(datastack,3)),2)); Std = std2(datastack); max = Mean + (s*Std); min = Mean - (s*Std); data = datastack; trig = triggers; list = zeros(length(triggers(:,2)),1); count = 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 (((datastack(j,k,i)) > max) || (datastack(j,k,i) < min)) count(i,1) = count(i,1) + 1; 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