function [peak, h8, LEBiStimThresEyePos, REBiStimThresEyePos, indexvectorEyePos,...
    output1S1,output1S2,peakLeMono,peakReMono,LELStimThresEyePos,RERStimThresEyePos,output1Left,output1Right]=...
    figure8subfunctionpos(DFFdec, handles)
% to ensure variable is defined
REBiStimThresEyePos=nan;
LEBiStimThresEyePos=nan;
LELStimThresEyePos=nan;
RERStimThresEyePos=nan;
peakLeMono=nan;
peakReMono=nan;

indexvectorEyePos=-39:2:39;

% tuning cure for Left eye monocular stimulation
h8=figure(8);
h81=subplot(3,2,1);
toTuning=nan(size(DFFdec(1:300),2),2);
toTuning(:,2)=DFFdec(1:300);
toTuning(:,1)=handles.desRfile(1:300,2)-8; %-8 to standardize Le and Re
[output1,output2]=averageEye_V3(toTuning); %bin data in 2° bins
output1(find(output1(:,2)==0),1)=NaN;
plot(-39:2:39,output1(:,1),'k')
hold on
toTuning(:,2)=zscore(DFFdec(1:300));
toTuning(:,1)=handles.desRfile(1:300,2)-8;
[output1S1,output2S1]=averageEye_V3(toTuning);
output1S1(find(output1S1(:,2)==0),1)=NaN;
plot(-39:2:39,output1S1(:,1),'r')
title('Left eye monocular stimulation; zscore red')
set(h81,'XLim',[-40 40])
clear toTuning

% tuning cure for Right eye monocular stimulation
h82=subplot(3,2,2);
toTuning=nan(size(DFFdec(301:600),2),2);
toTuning(:,2)=DFFdec(301:600);
toTuning(:,1)=handles.desRfile(301:600,3)+8; %+8 to standardize Le and Re
[output1,output2]=averageEye_V3(toTuning); %bin data in 2° bins
output1(find(output1(:,2)==0),1)=NaN;
plot(-39:2:39,output1(:,1),'k')
hold on
toTuning(:,2)=zscore(DFFdec(301:600));
toTuning(:,1)=handles.desRfile(301:600,3)+8;
[output1S2,output2S2]=averageEye_V3(toTuning);
output1S2(find(output1S2(:,2)==0),1)=NaN;
plot(-39:2:39,output1S2(:,1),'r')
title('Right eye monocular stimulation; zscore red')
set(h82,'XLim',[-40 40])
clear toTuning

% tuning cure for Left eye binocular stimulation
h83=subplot(3,2,3);
toTuning=nan(size(DFFdec(601:end),2),2);
toTuning(:,2)=DFFdec(601:end);
toTuning(:,1)=handles.desRfile(601:end,2)-8;
[output1,output2]=averageEye_V3(toTuning);
output1(find(output1(:,2)==0),1)=NaN;
plot(-39:2:39,output1(:,1),'k')
hold on
toTuning(:,2)=zscore(DFFdec(601:end));
toTuning(:,1)=handles.desRfile(601:end,2)-8;
[output1Left,outputRawLeft]=averageEye_V3(toTuning);
output1Left(find(output1Left(:,2)==0),1)=NaN;
plot(-39:2:39,output1Left(:,1),'r')
output1Left(:,3)=-39:2:39;
title('Left eye binocular stimulation; zscore red')
set(h83,'XLim',[-40 40])
clear output1
clear toTuning

% find peak (tuning orientation); 1==left
findpeakLeft=output1Left;
findpeakLeft(:,3)=-39.5:2:39.5;
findpeakLeft(isnan(findpeakLeft(:,1)),:)=[];
findpeakLeft=sortrows(findpeakLeft,1);
running=0;
for pfk=0:2
    if findpeakLeft(end-pfk,3)<mean(findpeakLeft(:,3))
        running=running-1; %remove one when peak is to the left
    else
        running=running+1; %add 1 when peak is to the right
    end
end

% tuning cure for Right eye binocular stimulation
h84=subplot(3,2,4);
toTuning=nan(size(DFFdec(601:end),2),2);
toTuning(:,2)=DFFdec(601:end);
toTuning(:,1)=handles.desRfile(601:end,3)+8;
[output1,output2]=averageEye_V3(toTuning);
output1(find(output1(:,2)==0),1)=NaN;
plot(-39:2:39,output1(:,1),'k')
hold on
toTuning(:,2)=zscore(DFFdec(601:end));
toTuning(:,1)=handles.desRfile(601:end,3)+8;
[output1Right,outputRawRight]=averageEye_V3(toTuning);
output1Right(find(output1Right(:,2)==0),1)=NaN;
plot(-39:2:39,output1Right(:,1),'r')
output1Right(:,3)=-39:2:39;
title('Right eye binocular stimulation; zscore red')
set(h84,'XLim',[-40 40])
clear toTuning

% find peak (tuning orientation); 1==left
findpeakRight=output1Right;
findpeakRight(:,3)=-39:2:39;
findpeakRight(isnan(findpeakRight(:,1)),:)=[];
findpeakRight=sortrows(findpeakRight,1);
running2=0;
for pfk=0:2
    if findpeakRight(end-pfk,3)<mean(findpeakRight(:,3))
        running2=running2-1; %remove one when peak is to the left
    else
        running2=running2+1; %add 1 when peak is to the right
    end
end

% plot activity and left eye postion
subplot(3,2,5)
plot(handles.desRfile(:,2)-8,'k')
title('eye pos for left eye (dec kernelled DFF red)')
hold on
plot(DFFdec*20,'r')

% plot activity and right eye postion
subplot(3,2,6)
plot(handles.desRfile(:,3)+8,'k')
hold on
title('eye pos for right eye (dec kernelled DFF red)')
plot(DFFdec*20,'r')

%parameters to save figure
set(h8,'PaperPositionMode','auto')
set(h8, 'units','normalized','outerposition',[0 0 1 1]);


% peak 1 := peak is to the right
% peak 0 := peak is to the left
if running>0 && running2>0
    peak=1;
elseif running<0 && running2<0
    peak=0;
else
    peak=NaN;
end


%  calculate statistical firing threshold for subplot 3 (LE BiStim)
[LEBiStimThresEyePos]=getFiringThesholdNew(peak,output1Left,outputRawLeft,h83);
 
%  calculate statistical firing threshold for subplot 4 (RE BiStim)
[REBiStimThresEyePos]=getFiringThesholdNew(peak,output1Right,outputRawRight,h84);
% end original subfunction

if ismember(cell2mat(handles.activeCell(1,67)),30:53) %check if neurons is from monocular exclusive regressors 
    %define peak for monocular neurons
    % for left eye 
    if ismember(cell2mat(handles.activeCell(1,67)),38:41) %LE Pos
        output1S1Mono=output1S1;
        output1S1Mono(:,3)=-39.5:2:39.5;
        output1S1Mono(isnan(output1S1Mono(:,1)),:)=[];
        output1S1Mono=sortrows(output1S1Mono,1);
        runningLeMono=0;
        for pfk=0:2
            if output1S1Mono(end-pfk,3)<mean(output1S1Mono(:,3))
                runningLeMono=runningLeMono-1;%remove one when peak is to the left
            else
                runningLeMono=runningLeMono+1;%add 1 when peak is to the right
            end
        end
        clear pfk
        
        if runningLeMono>0
            peakLeMono=1;
        elseif runningLeMono<0
            peakLeMono=0;
        end
        
        [LELStimThresEyePos]=getFiringThesholdNew(peakLeMono,output1S1,output2S1,h81);

    elseif ismember(cell2mat(handles.activeCell(1,67)),46:49) %RE Pos   
        output1S2Mono=output1S2;
        output1S2Mono(:,3)=-39.5:2:39.5;
        output1S2Mono(isnan(output1S2Mono(:,1)),:)=[];
        output1S2Mono=sortrows(output1S2Mono,1);
        runningReMono=0;
        for pfk=0:2
            if output1S2Mono(end-pfk,3)<mean(output1S2Mono(:,3))
                runningReMono=runningReMono-1;%remove one when peak is to the left
            else
                runningReMono=runningReMono+1;%add 1 when peak is to the right
            end
        end
        clear pfk
        
        if runningReMono>0
            peakReMono=1;
        elseif runningReMono<0
            peakReMono=0;
        end
        
       [RERStimThresEyePos]=getFiringThesholdNew(peakReMono,output1S2,output2S2,h82);

    end
end


end %function end