function [exportmatOutput, Successcrit]=MonoBinofig(handles, CellnumberInExportmatFile)
%to make sure previously opened figures are closed
for hhk=[1:13,21,31,41,51,100,110,120,130]  
    h=figure(hhk);
    close(h)
end
celldata=handles.activeCell;

%         Kernel DFF data
DFFOrigKernel=celldata{1,7};
        for k=3:(size(celldata{1,7},2)-2)
            DFFOrigKernel(1,k)=...
                (celldata{1, 7}(1,k)+...
                (celldata{1, 7}(1,k-1)*0.5)+...
                (celldata{1, 7}(1,k+1)*0.5)+...
                (celldata{1, 7}(1,k-2)*0.25)+...
                (celldata{1, 7}(1,k+2)*0.25))/2.5;
        end
        
    % deconvolve DFF
    tau=handles.tau;
    frametime=handles.frameTime;
    DFFtimevektor=frametime:frametime:frametime*size(DFFOrigKernel,2);
    decfunct=exp(-DFFtimevektor/tau); 
    DFFzeros=[zeros(1,length(DFFOrigKernel)) DFFOrigKernel zeros(1,length(DFFOrigKernel))];
    [QValues, ~]=deconv(DFFzeros,decfunct); %deconvolve
    DFFdec=QValues(size(DFFOrigKernel,2)+1:size(DFFOrigKernel,2)+1+size(DFFOrigKernel,2)-1);
    
    % get values
    %DFF
    Lstim=DFFdec(handles.edit_SLE:handles.edit_SRE);
    Rstim=DFFdec(handles.edit_SRE+1:handles.edit_SBE);
    Bistim=DFFdec(handles.edit_SBE+1:end);
    %standardize for individual resting position
    LstimLeye=handles.desRfile(handles.edit_SLE:handles.edit_SRE,2)-8;
    RstimLeye=handles.desRfile(handles.edit_SRE+1:handles.edit_SBE,2)-8;
    BistimLeye=handles.desRfile(handles.edit_SBE+1:end,2)-8;
    LstimReye=handles.desRfile(handles.edit_SLE:handles.edit_SRE,3)+8;
    RstimReye=handles.desRfile(handles.edit_SRE+1:handles.edit_SBE,3)+8;
    BistimReye=handles.desRfile(handles.edit_SBE+1:end,3)+8;

    
%% plot data
out=handles;
% plot tuning curves for position (figure8subfunctionpos) and velocity
% (figure9subfunctionvel) to extract firing thresholds
[peak, h8, LEBiStimThresEyePos, REBiStimThresEyePos, indexvectorEyePos,...
    output1S1,output1S2,peakLeMono,peakReMono,LELStimThresEyePos,RERStimThresEyePos,output1Left,output1Right]=...
    figure8subfunctionpos(DFFdec, out);
[peakvel, h9,  LEBiStimThresEyeVel, REBiStimThresEyeVel, indexvectorEyeVel,...
    output1S1vel,output1S2vel,peakLeMonoVel,peakReMonoVel,LELStimThresEyeVel,RERStimThresEyeVel,output1LeftVel,output1RightVel]=...
    figure9subfunctionvel(DFFdec, out);

%% specify cell type and exclude neurons depending on firing threshold
%corr threshold should already be checked in step one but just to be sure
if max(cell2mat(celldata(1,14:66)))>=handles.RegThres
    typecell=find(cell2mat(celldata(1,14:66))==max(cell2mat(celldata(1,14:66))));

elseif max(abs(cell2mat(celldata(1,14:66))))<handles.RegThres
    typecell=54;  
end

if typecell~=cell2mat(handles.activeCell(1,67))
    keyboard
end


%% check firing range

Thresnotreach=0;
%please note the additional regressor 1 (see Notes ind Readme files)
% check firing thresholds for position neurons (with binocular activity)
if (ismember(typecell,2:9) || ismember(typecell,18:23)) && ~isnan(REBiStimThresEyePos) && ~isnan(LEBiStimThresEyePos)...
        && ~isnan(peak)
    if exist('LEBiStimThresEyePos','var') && exist('REBiStimThresEyePos','var') &&...
            exist('peak','var')
                if peak==1 %peak right
                    if ~(indexvectorEyePos(find(~isnan(output1S1(:,1)),1,'last'))>LEBiStimThresEyePos)
                    Thresnotreach=1;
                    end
                    if ~(indexvectorEyePos(find(~isnan(output1S2(:,1)),1,'last'))>REBiStimThresEyePos)
                    Thresnotreach=1;
                    end
                elseif peak==0
                    if ~(indexvectorEyePos(find(~isnan(output1S1(:,1)),1,'first'))<LEBiStimThresEyePos)
                    Thresnotreach=1;
                    end
                    
                    if ~(indexvectorEyePos(find(~isnan(output1S2(:,1)),1,'first'))<REBiStimThresEyePos)
                    Thresnotreach=1;
                    end
                else 
                    FORCEDERROR
                end
    end
% check firing thresholds for velocity neurons (with binocular activity)    
elseif (ismember(typecell,10:17) || ismember(typecell,24:29)) && ~isnan(REBiStimThresEyeVel) && ~isnan(LEBiStimThresEyeVel)...
        && ~isnan(peakvel)
        if exist('LEBiStimThresEyeVel','var') && exist('REBiStimThresEyeVel','var') &&...
            exist('peak','var')
            if peakvel==1
               if ~(indexvectorEyeVel(find(~isnan(output1S1vel(:,1)),1,'last'))>LEBiStimThresEyeVel)
                Thresnotreach=1;
               end
               if ~(indexvectorEyeVel(find(~isnan(output1S2vel(:,1)),1,'last'))>REBiStimThresEyeVel)
                Thresnotreach=1;
               end               
            elseif peakvel==0
                if ~(indexvectorEyeVel(find(~isnan(output1S1vel(:,1)),1,'first'))<LEBiStimThresEyeVel)
                    Thresnotreach=1;
                end
                if ~(indexvectorEyeVel(find(~isnan(output1S2vel(:,1)),1,'first'))<REBiStimThresEyeVel)
                    Thresnotreach=1;
                end
            else
                FORCEDERROR
            end
        end
% check firing thresholds for position neurons (MLEE)
elseif ismember(typecell,38:41) && ~isnan(LELStimThresEyePos) && ~isnan(peakLeMono)
    if peakLeMono==1   
        if ~(indexvectorEyePos(find(~isnan(output1S2(:,1)),1,'last'))>LELStimThresEyePos)
            Thresnotreach=1;
        end
        if ~(indexvectorEyePos(find(~isnan(output1Left(:,1)),1,'last'))>LELStimThresEyePos)
            Thresnotreach=1;
        end              
    elseif peakLeMono==0
         if ~(indexvectorEyePos(find(~isnan(output1S2(:,1)),1,'first'))<LELStimThresEyePos)
            Thresnotreach=1;
         end     
         if ~(indexvectorEyePos(find(~isnan(output1Left(:,1)),1,'first'))<LELStimThresEyePos)
            Thresnotreach=1;
         end
    else
        FORCEDERROR
    end
% check firing thresholds for position neurons (MREE)    
elseif ismember(typecell,46:49) && ~isnan(RERStimThresEyePos) && ~isnan(peakReMono)
    if peakReMono==1   
        if ~(indexvectorEyePos(find(~isnan(output1S1(:,1)),1,'last'))>RERStimThresEyePos)
            Thresnotreach=1;
        end
        if ~(indexvectorEyePos(find(~isnan(output1Right(:,1)),1,'last'))>RERStimThresEyePos)
            Thresnotreach=1;
        end              
    elseif peakReMono==0
         if ~(indexvectorEyePos(find(~isnan(output1S1(:,1)),1,'first'))<RERStimThresEyePos)
            Thresnotreach=1;
         end     
         if ~(indexvectorEyePos(find(~isnan(output1Right(:,1)),1,'first'))<RERStimThresEyePos)
            Thresnotreach=1;
         end
    else
        FORCEDERROR
    end

% check firing thresholds for velocity neurons (MLEE)
elseif ismember(typecell,42:45) && ~isnan(LELStimThresEyeVel) && ~isnan(peakLeMonoVel)    
      if peakLeMonoVel==1
               if ~(indexvectorEyeVel(find(~isnan(output1S2vel(:,1)),1,'last'))>LELStimThresEyeVel)
                Thresnotreach=1;
               end
               if ~(indexvectorEyeVel(find(~isnan(output1LeftVel(:,1)),1,'last'))>LELStimThresEyeVel)
                Thresnotreach=1;
               end               
      elseif peakLeMonoVel==0
                if ~(indexvectorEyeVel(find(~isnan(output1S2vel(:,1)),1,'first'))<LELStimThresEyeVel)
                    Thresnotreach=1;
                end
                if ~(indexvectorEyeVel(find(~isnan(output1LeftVel(:,1)),1,'first'))<LELStimThresEyeVel)
                    Thresnotreach=1;
                end
      else
          FORCEDERROR
      end
% check firing thresholds for velocity neurons (MREE)
elseif ismember(typecell,50:53) && ~isnan(RERStimThresEyeVel) && ~isnan(peakReMonoVel)      
    if peakReMonoVel==1  
       if ~(indexvectorEyeVel(find(~isnan(output1S1vel(:,1)),1,'last'))>RERStimThresEyeVel)
          Thresnotreach=1;
       end
       if ~(indexvectorEyeVel(find(~isnan(output1RightVel(:,1)),1,'last'))>RERStimThresEyeVel)
          Thresnotreach=1;
       end 
    elseif peakReMonoVel==0
        
           if ~(indexvectorEyeVel(find(~isnan(output1S1vel(:,1)),1,'first'))<RERStimThresEyeVel)
              Thresnotreach=1;
           end
           if ~(indexvectorEyeVel(find(~isnan(output1RightVel(:,1)),1,'first'))<RERStimThresEyeVel)
              Thresnotreach=1;
           end
                
    else
        FORCEDERROR
    end
% check firing thresholds for position neurons (ME)    
elseif ismember(typecell,30:33) && isequal(peakReMono,peakLeMono) && ~isnan(LELStimThresEyePos) && ~isnan(RERStimThresEyePos)
    if peakReMono==1
       if ~(indexvectorEyePos(find(~isnan(output1Left(:,1)),1,'last'))>LELStimThresEyePos)
            Thresnotreach=1;
       end
       if ~(indexvectorEyePos(find(~isnan(output1Right(:,1)),1,'last'))>RERStimThresEyePos)
            Thresnotreach=1;
       end    
    elseif peakReMono==0
         if ~(indexvectorEyePos(find(~isnan(output1Left(:,1)),1,'first'))<LELStimThresEyePos)
            Thresnotreach=1;
         end
         if ~(indexvectorEyePos(find(~isnan(output1Right(:,1)),1,'first'))<RERStimThresEyePos)
            Thresnotreach=1;
         end
    end
else % no check firing thresholds for velcoity neurons (ME) as none are identified in S1
  Thresnotreach=1;  
  disp('notspecifiedcelltypeOrNoPeak')
end

% Plot Regressor identifier in red or green based if accepted or rejected
if Thresnotreach==0
annotation(h8,'textbox',[0.45 0.89 0.1 0.1],'String',strcat('\bf',num2str(typecell)),'Units','normalized',...
        'HorizontalAlignment','center','VerticalAlignment', 'top','Fontsize',14,'Color','g','Linestyle','none');
annotation(h9,'textbox',[0.45 0.89 0.1 0.1],'String',strcat('\bf',num2str(typecell)),'Units','normalized',...
        'HorizontalAlignment','center','VerticalAlignment', 'top','Fontsize',14,'Color','g','Linestyle','none');
else
annotation(h8,'textbox',[0.45 0.89 0.1 0.1],'String',strcat('\bf',num2str(typecell)),'Units','normalized',...
        'HorizontalAlignment','center','VerticalAlignment', 'top','Fontsize',14,'Color','r','Linestyle','none');
annotation(h9,'textbox',[0.45 0.89 0.1 0.1],'String',strcat('\bf',num2str(typecell)),'Units','normalized',...
        'HorizontalAlignment','center','VerticalAlignment', 'top','Fontsize',14,'Color','r','Linestyle','none');    
end

%crop last 4 characters of filename (would cause error otherwise if saved)
temp=handles.activeCell{1,1}(1,2);
temp=cell2mat(temp);
temp=temp(1:end-4);


if CellnumberInExportmatFile==1
    for indxkr=1:54 %create folders for each regressor number
       mkdir(fullfile(handles.Savepath_S2,'\S2\approved'),num2str(indxkr))
       mkdir(fullfile(handles.Savepath_S2,'\S2\failed'),num2str(indxkr))
    end  
end



if Thresnotreach==0    
   saveas(h8,fullfile(handles.Savepath_S2,strcat('\S2\approved\',num2str(typecell)),...
        strcat(temp,'Cellnumber_',num2str(handles.activeCell{1,1}{1,3}),'_Expnr',num2str(CellnumberInExportmatFile),'F8')),'fig')
   saveas(h8,fullfile(handles.Savepath_S2,strcat('\S2\approved\',num2str(typecell)),...
        strcat(temp,'Cellnumber_',num2str(handles.activeCell{1,1}{1,3}),'_Expnr',num2str(CellnumberInExportmatFile),'F8')),'jpg')
   saveas(h9,fullfile(handles.Savepath_S2,strcat('\S2\approved\',num2str(typecell)),...
        strcat(temp,'Cellnumber_',num2str(handles.activeCell{1,1}{1,3}),'_Expnr',num2str(CellnumberInExportmatFile),'F9')),'fig')
   saveas(h9,fullfile(handles.Savepath_S2,strcat('\S2\approved\',num2str(typecell)),...
        strcat(temp,'Cellnumber_',num2str(handles.activeCell{1,1}{1,3}),'_Expnr',num2str(CellnumberInExportmatFile),'F9')),'jpg')
elseif Thresnotreach==1
       saveas(h8,fullfile(handles.Savepath_S2,strcat('\S2\failed\',num2str(typecell)),...
        strcat(temp,'Cellnumber_',num2str(handles.activeCell{1,1}{1,3}),'_Expnr',num2str(CellnumberInExportmatFile),'F8')),'fig')
   saveas(h8,fullfile(handles.Savepath_S2,strcat('\S2\failed\',num2str(typecell)),...
        strcat(temp,'Cellnumber_',num2str(handles.activeCell{1,1}{1,3}),'_Expnr',num2str(CellnumberInExportmatFile),'F8')),'jpg')
   saveas(h9,fullfile(handles.Savepath_S2,strcat('\S2\failed\',num2str(typecell)),...
        strcat(temp,'Cellnumber_',num2str(handles.activeCell{1,1}{1,3}),'_Expnr',num2str(CellnumberInExportmatFile),'F9')),'fig')
   saveas(h9,fullfile(handles.Savepath_S2,strcat('\S2\failed\',num2str(typecell)),...
        strcat(temp,'Cellnumber_',num2str(handles.activeCell{1,1}{1,3}),'_Expnr',num2str(CellnumberInExportmatFile),'F9')),'jpg')
end

% plot for Figure 3 left eye
DoubleMeshPlot(21,LstimLeye,LstimReye,true(300,1),Lstim,BistimLeye,BistimReye,...
    true(299,1),Bistim,'Lstim');

% plot for Figure 3 left eye
DoubleMeshPlot(51,RstimReye,RstimLeye,true(300,1),Rstim,BistimLeye,BistimReye,...
    true(299,1),Bistim,'Rstim');


%% save figures
    k=handles.k;
    
    if CellnumberInExportmatFile==1 %create folders
        for fik=1:54
            mkdir(strcat(handles.Savepath_S2,'\CompareTuning\',num2str(fik),'\Le'))
            mkdir(strcat(handles.Savepath_S2,'\CompareTuning\',num2str(fik),'\Re'))
        end
    end

saveas(21,fullfile(strcat(handles.Savepath_S2,'\CompareTuning\',num2str(typecell),'\Le'),strcat('LeftEye_Nr',num2str(k))),'fig')
saveas(21,fullfile(strcat(handles.Savepath_S2,'\CompareTuning\',num2str(typecell),'\Le'),strcat('LeftEye_Nr',num2str(k))),'jpg')
saveas(51,fullfile(strcat(handles.Savepath_S2,'\CompareTuning\',num2str(typecell),'\Re'),strcat('RightEye_Nr',num2str(k))),'fig')
saveas(51,fullfile(strcat(handles.Savepath_S2,'\CompareTuning\',num2str(typecell),'\Re'),strcat('RightEye_Nr',num2str(k))),'jpg')


%     export if firing thrsholds were reached
if Thresnotreach==0 
    Successcrit=1;
else
    Successcrit=0;
end

% add firingthreholds to data file
    celldata{1, 1}{1, 4}=LEBiStimThresEyePos;
    celldata{1, 1}{1, 5}=REBiStimThresEyePos;
    celldata{1, 1}{1, 6}=peak;
    celldata{1, 1}{1, 7}=LEBiStimThresEyeVel;
    celldata{1, 1}{1, 8}=REBiStimThresEyeVel;
    celldata{1, 1}{1, 9}=peakvel;
    celldata{1, 1}{1, 10}=peakLeMono;
    celldata{1, 1}{1, 11}=peakReMono;
    celldata{1, 1}{1, 12}=LELStimThresEyePos;
    celldata{1, 1}{1, 13}=RERStimThresEyePos;
    celldata{1, 1}{1, 14}=peakLeMonoVel;
    celldata{1, 1}{1, 15}=peakReMonoVel;
    celldata{1, 1}{1, 16}=LELStimThresEyeVel;
    celldata{1, 1}{1, 17}=RERStimThresEyeVel;
    
exportmatOutput=celldata;
end %function end
