function [data, trig, list, count] = exclude_outliers_std2(datastack, triggers, s, c) %this function counts how many data points in each epoch are not within s %standard deviations of the overall mean of the data - this is the count %vector. then the epochs for which this count is not within c standard %deviations of the mean count are excluded. the data and trig are the new %datastack and trigger vector without these epochs. the list is a list of %the indices of those epochs which were excluded. 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; end end end end Mean2 = mean(count); Std2 = std(count); Max = Mean2 + c*Std2; for i = length(datastack(1,1,:)):-1:1 if count(i,1) > Max data(:,:,i) = []; trig(i,:) = []; list(m,1) = i; m = m + 1; end end for i = length(triggers(:,2)):-1:1 if list(i,1) == 0 list(i,:) = []; end end end