function [Var, datastack, List] = exclude_by_std(datastack, variable, r) % This function excludes values of a variable that are more than r standard deviations % away from the mean. Makes a new datastack excluding those epochs which % correspond to the excluded variable indices. Also makes a list of those % indices. Mean = mean(variable); Std = std(variable); Max = Mean + r*Std; Min = Mean - r*Std; Var = variable; List = zeros(length(Var(:,1)),1); m = 1; for i = length(variable(:,1)):-1:1 if ((Var(i,1) > Max) || (Var(i,1) < Min)) Var(i,:) = []; List(m,1) = i; m = m+1; datastack(:,:,i) = []; end end for i = length(variable(:,1)):-1:1 if List(i,1) == 0 List(i,:) = []; end end end