function [output]=updateCellList_for3Regressors(RegionStats, selection, regnbr, regValue, handles)
%-------------------------------------------------------------------%
% input: RegionStats = RegionStats
% input: selection = boolean if any selections should be made
% input: regnbr = regressor number for regValue OBSOLETE
% input: regValue = min and max threshold used to exclude cells from
% analysis OBSOLETE
% input: handles = handle structure to extract values for other regressors
%-------------------------------------------------------------------%
R2i=handles.edit_incR2; % code otherwise a lil long OBSOLETE
R2v=handles.edit_incV2; % code otherwise a lil long OBSOLETE

List=1:size(RegionStats,1);
List=List';
if selection==0 
   output=List;  
elseif selection==1
    if handles.check_enableReg==1
        List(:,2)=NaN; %2nd column will be used to label which cells to keep (1); 0 is discard
        for k=1:size(RegionStats,1)

%% Code with rectified Position and velocity regressors 
%Position
PosRecPos=zeros(1,size(handles.R,1));
PosRecNeg=zeros(1,size(handles.R,1));
PosRecPos(1,handles.R(:,1)>=0)=handles.R(handles.R(:,1)>=0,1);
PosRecNeg(1,handles.R(:,1)<0)=handles.R(handles.R(:,1)<0,1);
PosRecNeg=PosRecNeg*(-1);

%convolve
frameTime=0.5;
time=frameTime:frameTime:frameTime*size(handles.R,1);
% Convolve regressors with an exponential impulse response
CIRF=exp(-time/1.1); %define the calcium impulse response function
zeroPadded = [zeros(1,size(PosRecPos,2)) PosRecPos zeros(1,size(PosRecPos,2))]; %pad the regressor with zeros prioir to convolution
PosRecPos= conv(zeroPadded,CIRF);
PosRecPos=PosRecPos(size(time,2)+1:size(time,2)*2);

zeroPadded = [zeros(1,size(PosRecNeg,2)) PosRecNeg zeros(1,size(PosRecNeg,2))]; %pad the regressor with zeros prioir to convolution
PosRecNeg= conv(zeroPadded,CIRF);
PosRecNeg=PosRecNeg(size(time,2)+1:size(time,2)*2);


%Velocity
VelRecPos=zeros(1,size(handles.R,1));
VelRecNeg=zeros(1,size(handles.R,1));
padVel=diff(handles.R(:,1));

VelRecPos(1,padVel>=0)=padVel(padVel>=0);
VelRecPos(2:end)=VelRecPos(1:end-1);
VelRecPos(1,1)=0; %to account for differential regessor (one smaller than DFF trace)
VelRecPos(VelRecPos>4)=4;

VelRecNeg(1,padVel<0)=padVel(padVel<0);
VelRecNeg=VelRecNeg*(-1);
VelRecNeg(2:end)=VelRecNeg(1:end-1);
VelRecNeg(1,1)=0; %to account for differential regessor (one smaller than DFF trace)
VelRecNeg(VelRecNeg>4)=4;


zeroPadded = [zeros(1,size(VelRecPos,2)) VelRecPos zeros(1,size(VelRecPos,2))]; %pad the regressor with zeros prioir to convolution
VelRecPos= conv(zeroPadded,CIRF);
VelRecPos=VelRecPos(size(time,2)+1:size(time,2)*2);

zeroPadded = [zeros(1,size(VelRecNeg,2)) VelRecNeg zeros(1,size(VelRecNeg,2))]; %pad the regressor with zeros prioir to convolution
VelRecNeg= conv(zeroPadded,CIRF);
VelRecNeg=VelRecNeg(size(time,2)+1:size(time,2)*2);
       
%check correlation
checkvec=[corr(RegionStats(k).DFF',PosRecPos')>0.4...
    corr(RegionStats(k).DFF',PosRecNeg')>0.4,...
    corr(RegionStats(k).DFF',VelRecPos')>0.4,...
    corr(RegionStats(k).DFF',VelRecNeg')>0.4];
if any(checkvec)
    List(k,2)=1;
else
    List(k,2)=0;
end
clear t1 t2 t3 checkvec
        end
    List(find(List(:,2)==0),:)=[];
    List(:,2)=[];
    output=List;
    end %end regressor selection
%% End of code with rectified Position and velocity regressors 
 %% spatial position selection 
 % the following part might not be up to data, but it  was not used in the
 % data analysis
 
    if handles.check_enablePos==1
       List(:,2)=NaN;
       for k=1:size(List,1)
           running=1;
           %x axis (left-right)
           if (cell2mat(handles.Exportmat(List(k,1),2))<= handles.edit_latRight) &&...
                   (cell2mat(handles.Exportmat(List(k,1),2))>= handles.edit_latLeft)
               List(k,2)=1;
           else
               List(k,2)=0;
               running=0;
           end
          
           %y axis (rostro-caudal)
           if (cell2mat(handles.Exportmat(List(k,1),3))<= handles.edit_rostral) &&...
                   (cell2mat(handles.Exportmat(List(k,1),3))>= handles.edit_caudal) &&...
                   running==1
               List(k,2)=1; %unneccessary 
           else
               List(k,2)=0;
               running=0;
           end
           
           %z axis (dorso-ventral)
           if (cell2mat(handles.Exportmat(List(k,1),4))<= handles.edit_dorsal) &&...
                   (cell2mat(handles.Exportmat(List(k,1),4))>= handles.edit_ventral) &&...
                   running==1
               List(k,2)=1; %unneccessary 
           else
               List(k,2)=0;
               running=0;
           end
           clear running %to be safe
       end %end k loop
    List(find(List(:,2)==0),:)=[];
    List(:,2)=[];
    output=List;       
    end %end position selection
else
    FORCEDERROR
end %selection end

end%function end