function [output,temp]=findCellTypeMBRectified(handles,CellnumberinExpFile)

endBE=size(handles.currentS1ROI{1,8},1); %change this if more than 3 phases are used!

%         Kernel DFF
currentS1ROIKernell=handles.currentS1ROI{1,7};
        for k=3:(size(handles.currentS1ROI{1, 7},2)-2)
            currentS1ROIKernell(1,k)=...
                (handles.currentS1ROI{1, 7}(1,k)+...
                (handles.currentS1ROI{1, 7}(1,k-1)*0.5)+...
                (handles.currentS1ROI{1, 7}(1,k+1)*0.5)+...
                (handles.currentS1ROI{1, 7}(1,k-2)*0.25)+...
                (handles.currentS1ROI{1, 7}(1,k+2)*0.25))/2.5;
        end


temp=cell2mat(handles.currentS1ROI{1,1}(1,2));
temp=temp(1:end-4);
temp=strcat(temp,'_cellnumber_',num2str(CellnumberinExpFile));
%% Regressors

LeOFF=median(handles.currentS1R(:,2)); %standardize Le % Re so that 0 = neutral eye position
ReOFF=median(handles.currentS1R(:,3));
NewLe=handles.currentS1R(:,2)-LeOFF;
NewRe=handles.currentS1R(:,3)-ReOFF;


% Position
% Be
NewBe=(NewRe+NewLe)/2;
NewBePos=zeros(1,size(NewBe,1));
NewBeNeg=zeros(1,size(NewBe,1));
NewBePos(1,NewBe>0)=NewBe(NewBe>0,1);
NewBeNeg(1,NewBe<0)=NewBe(NewBe<0,1);
NewBeNeg=NewBeNeg*-1;

% 1st convolution 
frameTime=0.5;
time=frameTime:frameTime:frameTime*size(handles.currentS1R,1);
% Convolve regressors with an exponential impulse response
CIRF=exp(-time/1.1); %define the calcium impulse response function

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

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


% Re
NewRePos=zeros(1,size(NewRe,1));
NewReNeg=zeros(1,size(NewRe,1));
NewRePos(1,NewRe>0)=NewRe(NewRe>0,1);
NewReNeg(1,NewRe<0)=NewRe(NewRe<0,1);
NewReNeg=NewReNeg*-1;

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

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


% Le
NewLePos=zeros(1,size(NewLe,1));
NewLeNeg=zeros(1,size(NewLe,1));
NewLePos(1,NewLe>0)=NewLe(NewLe>0,1);
NewLeNeg(1,NewLe<0)=NewLe(NewLe<0,1);
NewLeNeg=NewLeNeg*-1;

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

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


% Velocity
% Left Eye
LeLowVel=diff(handles.currentS1R(:,2));
LeLowVel(2:end+1)=LeLowVel;
LeLowVel(1,1)=0; %to account for smaller differential based velocity
LeLowVel(find(LeLowVel>=4),1)=4; 
LeLowVel(find(LeLowVel<=-4),1)=-4;


LeLowVelCW=zeros(1,size(LeLowVel,1));
LeLowVelCCW=zeros(1,size(LeLowVel,1));
LeLowVelCW(LeLowVel>0)=LeLowVel(LeLowVel>0);
LeLowVelCCW(LeLowVel<0)=LeLowVel(LeLowVel<0);
LeLowVelCCW=LeLowVelCCW*-1;

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

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

% Right Eye
ReLowVel=diff(handles.currentS1R(:,3));
ReLowVel(2:end+1)=ReLowVel;
ReLowVel(1,1)=0; %to account for smaller differential based velocity
ReLowVel(find(ReLowVel>=4),1)=4;
ReLowVel(find(ReLowVel<=-4),1)=-4;

ReLowVelCW=zeros(1,size(ReLowVel,1));
ReLowVelCCW=zeros(1,size(ReLowVel,1));
ReLowVelCW(ReLowVel>0)=ReLowVel(ReLowVel>0);
ReLowVelCCW(ReLowVel<0)=ReLowVel(ReLowVel<0);
ReLowVelCCW=ReLowVelCCW*-1;

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

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

%Be

BeLowVel=(ReLowVel+LeLowVel)/2;
BeLowVelCW=zeros(1,size(BeLowVel,1));
BeLowVelCCW=zeros(1,size(BeLowVel,1));
BeLowVelCW(BeLowVel>0)=BeLowVel(BeLowVel>0);
BeLowVelCCW(BeLowVel<0)=BeLowVel(BeLowVel<0);
BeLowVelCCW=BeLowVelCCW*-1;

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

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

%% create regressors
% please note the additional regressor 1 which was excluded from the final
% analysis as no neuron was identified as this regressor did not carry any
% information

% Position Regressors with binocular activity
WholeReg=zeros(size(handles.currentS1ROI{1, 8},1),53); %create Regressors R1=000
WholeReg(1,1)=0.001; % Otherwise no STD/Variance and result is NAN
WholeReg(600:899,2)=NewBePosconv(1,600:899); % R2=001 positive;
WholeReg(600:899,3)=NewBeNegconv(1,600:899); % R3=001 negative;
WholeReg(300:599,4)=NewRePosconv(1,300:599); % R4=011 positive
WholeReg(600:899,4)=NewBePosconv(1,600:899); 
WholeReg(300:599,5)=NewReNegconv(1,300:599); % R5=011 negative
WholeReg(600:899,5)=NewBeNegconv(1,600:899); 
WholeReg(1:299,6)=NewLePosconv(1,1:299);% R6=111 positive
WholeReg(300:599,6)=NewRePosconv(1,300:599);
WholeReg(600:899,6)=NewBePosconv(1,600:899); 
WholeReg(1:299,7)=NewLeNegconv(1,1:299);% R7=111 negative
WholeReg(300:599,7)=NewReNegconv(1,300:599);
WholeReg(600:899,7)=NewBeNegconv(1,600:899); 
WholeReg(1:299,8)=NewLePosconv(1,1:299);% R8=101 positive
WholeReg(600:899,8)=NewBePosconv(1,600:899);
WholeReg(1:299,9)=NewLeNegconv(1,1:299);% R9=101 negative
WholeReg(600:899,9)=NewBeNegconv(1,600:899);

% Velocity Regressors with binocular activity
WholeReg(600:899,10)=BeLowVelCWconv(1,600:899);% R10=001 positive
WholeReg(600:899,11)=BeLowVelCCWconv(1,600:899);% R11=001 negative
WholeReg(600:899,12)=BeLowVelCWconv(1,600:899);% R12=011 positive
WholeReg(300:599,12)=ReLowVelCWconv(1,300:599);% R12=011 positive
WholeReg(600:899,13)=BeLowVelCCWconv(1,600:899);% R13=011 negative
WholeReg(300:599,13)=ReLowVelCCWconv(1,300:599);% R13=011 negative
WholeReg(600:899,14)=BeLowVelCWconv(1,600:899);% R14=111 positive
WholeReg(300:599,14)=ReLowVelCWconv(1,300:599);% R14=111 positive
WholeReg(1:299,14)=LeLowVelCWconv(1,1:299);% R14=111 positive
WholeReg(600:899,15)=BeLowVelCCWconv(1,600:899);% R15=111 negative
WholeReg(300:599,15)=ReLowVelCCWconv(1,300:599);% R15=111 negative
WholeReg(1:299,15)=LeLowVelCCWconv(1,1:299);% R15=111 negative
WholeReg(1:299,16)=LeLowVelCWconv(1,1:299);% R16=101 positive
WholeReg(600:899,16)=BeLowVelCWconv(1,600:899);% R16=101 positive
WholeReg(1:299,17)=LeLowVelCCWconv(1,1:299);% R17=101 negative
WholeReg(600:899,17)=BeLowVelCCWconv(1,600:899);% R17=101 negative


% Position regressors with binocular activity averaged
WholeReg(1:299,18)=NewBePosconv(1,1:299);% R18=111 positive
WholeReg(300:599,18)=NewBePosconv(1,300:599);
WholeReg(600:899,18)=NewBePosconv(1,600:899); 
WholeReg(1:299,19)=NewBeNegconv(1,1:299);% R19=111 negative
WholeReg(300:599,19)=NewBeNegconv(1,300:599);
WholeReg(600:899,19)=NewBeNegconv(1,600:899); 
WholeReg(1:299,20)=NewBePosconv(1,1:299);% R20=101 positive
WholeReg(600:899,20)=NewBePosconv(1,600:899);
WholeReg(1:299,21)=NewBeNegconv(1,1:299);% R21=101 negative
WholeReg(600:899,21)=NewBeNegconv(1,600:899);
WholeReg(300:599,22)=NewBePosconv(1,300:599); % R22=011 positive
WholeReg(600:899,22)=NewBePosconv(1,600:899); 
WholeReg(300:599,23)=NewBeNegconv(1,300:599); % R23=011 negative
WholeReg(600:899,23)=NewBeNegconv(1,600:899); 

% Velocity regressors with binocular activity averaged
WholeReg(600:899,24)=BeLowVelCWconv(1,600:899);% R24=111 positive
WholeReg(300:599,24)=BeLowVelCWconv(1,300:599);% R24=111 positive
WholeReg(1:299,24)=BeLowVelCWconv(1,1:299);% R24=111 positive
WholeReg(600:899,25)=BeLowVelCCWconv(1,600:899);% R25=111 negative
WholeReg(300:599,25)=BeLowVelCCWconv(1,300:599);% R25=111 negative
WholeReg(1:299,25)=BeLowVelCCWconv(1,1:299);% R25=111 negative
WholeReg(1:299,26)=BeLowVelCWconv(1,1:299);% R26=101 positive
WholeReg(600:899,26)=BeLowVelCWconv(1,600:899);% R26=101 positive
WholeReg(1:299,27)=BeLowVelCCWconv(1,1:299);% R27=101 negative
WholeReg(600:899,27)=BeLowVelCCWconv(1,600:899);% R27=101 negative
WholeReg(600:899,28)=BeLowVelCWconv(1,600:899);% R28=011 positive
WholeReg(300:599,28)=BeLowVelCWconv(1,300:599);% R28=011 positive
WholeReg(600:899,29)=BeLowVelCCWconv(1,600:899);% R29=011 negative
WholeReg(300:599,29)=BeLowVelCCWconv(1,300:599);% R29=011 negative


% Position Regressors ME
WholeReg(1:299,30)=NewLePosconv(1,1:299);% R30=110 positive
WholeReg(300:599,30)=NewRePosconv(1,300:599);% R30=110 positive
WholeReg(1:299,31)=NewLeNegconv(1,1:299);% R31=110 negative
WholeReg(300:599,31)=NewReNegconv(1,300:599);% R31=110 negative
WholeReg(1:299,32)=NewBePosconv(1,1:299);% R32=110 positive A
WholeReg(300:599,32)=NewBePosconv(1,300:599);% R32=110 positive A
WholeReg(1:299,33)=NewBeNegconv(1,1:299);% R33=110 negative A
WholeReg(300:599,33)=NewBeNegconv(1,300:599);% R33=110 negative A

% Velocity Regressors ME
WholeReg(1:299,34)=LeLowVelCWconv(1,1:299);%R34=110 Vel positive
WholeReg(300:599,34)=ReLowVelCWconv(1,300:599);%R34=110 Vel positive
WholeReg(1:299,35)=LeLowVelCCWconv(1,1:299);%R35=110 Vel negative
WholeReg(300:599,35)=ReLowVelCCWconv(1,300:599);%R35=110 Vel negative
WholeReg(1:299,36)=BeLowVelCWconv(1,1:299);%R36=110 Vel positive A
WholeReg(300:599,36)=BeLowVelCWconv(1,300:599);%R36=110 Vel positive A
WholeReg(1:299,37)=BeLowVelCCWconv(1,1:299);%R37=110 Vel negative A
WholeReg(300:599,37)=BeLowVelCCWconv(1,300:599);%R37=110 Vel negative A

% MLEE regressors
WholeReg(1:299,38)=NewLePosconv(1,1:299);%R38 100 positive 
WholeReg(1:299,39)=NewLeNegconv(1,1:299);%R39 100 negative
WholeReg(1:299,40)=NewBePosconv(1,1:299);%R40 100 positive A
WholeReg(1:299,41)=NewBeNegconv(1,1:299);%R41 100 negative A
WholeReg(1:299,42)=LeLowVelCWconv(1:299);%42 100 Vel positive
WholeReg(1:299,43)=LeLowVelCCWconv(1:299);%43 100 Vel negative
WholeReg(1:299,44)=BeLowVelCWconv(1:299);%44 100 Vel positive A
WholeReg(1:299,45)=BeLowVelCCWconv(1:299);%45 100 Vel negative A

% MREE regressors
WholeReg(300:599,46)=NewRePosconv(1,300:599);%46 010 positive
WholeReg(300:599,47)=NewReNegconv(1,300:599);%47 010 negative
WholeReg(300:599,48)=NewBePosconv(1,300:599);%48 010 positive A
WholeReg(300:599,49)=NewBeNegconv(1,300:599);%49 010 negative A
WholeReg(300:599,50)=ReLowVelCWconv(1,300:599);%50 010 Vel positive
WholeReg(300:599,51)=ReLowVelCCWconv(1,300:599);%51 010 Vel negative 
WholeReg(300:599,52)=BeLowVelCWconv(1,300:599);%52 010 Vel positive A
WholeReg(300:599,53)=BeLowVelCCWconv(1,300:599);%53 010 Vel negative A

% correlate
for a=1:53
    WholeRegRes(1,a)=corr(WholeReg(:,a),currentS1ROIKernell');
end

%create outputfile
WholeRegressorRaw(1:12)=handles.currentS1ROI;
WholeRegressorRaw(1,13)={'Now corr for each neuron type'};
for c=1:53
    WholeRegressorRaw(1,13+c)={WholeRegRes(1,c)};
end
clear WholeRegRes

if handles.check_manualCell==0 
    WholeRegressorRaw(1,67)={99999};  % will be overwritten later
elseif handles.check_manualCell==1
    prompt='Specify cell type';
    WholeRegressorRaw(1,43)={input(prompt)};
end

output=WholeRegressorRaw;
end %function end
