function lockmode(hObject, eventdata, Empty_Button,num, Footnote,LockMode, AxesAFS, AxesSOL, Plotsettings,usmn, usmf,calcp)

lcs = [1 0 0;0 0.5 0;0 0 1;0 0.5 0.5;0.5 0 0.5;0.5 0.5 0];

actpwd = pwd;
cd(calcp);

data = get(Empty_Button,'UserData');


data.numb_segmentsL2=0;
set(Empty_Button,'UserData',data);

if (isfield(data,'ismatrix')==0) || (data.ismatrix == 0)
    if data.ismatrix == 0 set(Footnote,'String',' Warning, no data has been loaded.','ForegroundColor','red');return;end
end

if num == 1
    if (isfield(data,'FixPoint1_B') && size(data.FixPoint1_B,1)>0)
   

       if data.scalBorg==1
                FixPoint1=data.FixPoint1_B;
                FP=data.FP_B;
                IP=data.IP_B;
                segments=data.segments_B;
                mima=data.mima_B;
            else
                FixPoint1=data.FixPoint1_SV;
                FP=data.FP_SV;
                IP=data.IP_SV;
                segments=data.segments_SV;
                mima=data.mima_SV;
       end
       if data.numb_seg==1
            
            
            if inpolygon(FixPoint1(1),FixPoint1(2),FP(:,1),FP(:,2))==0
                data.isExpert1=0;
                data.FixPoint1_B=[];
                data.FixPoint1_SV=[];
                set(Empty_Button,'UserData',data);
                cd(actpwd);
                return;
            end
            [a,b]=inpolygon(FixPoint1(1),FixPoint1(2),segments{1}(:,1),segments{1}(:,2));
            if(a==1 && b==0)
                data.isExpert1=0;
                data.FixPoint1_B=[];
                data.FixPoint1_SV=[];
                set(Empty_Button,'UserData',data);
                cd(actpwd);
                return;
            end
        else
            
            test=0;
            for i=1:data.numb_seg
                if inpolygon(FixPoint1(1),FixPoint1(2),segments{i}(:,1),segments{i}(:,2))==1
                    test=test+1;
                end
            end
            if test==0
                data.isExpert1=0;
                set(Empty_Button,'UserData',data);
                cd(actpwd);
                return;
            end
       end
       
       set(gcf,'Pointer','Watch');
            
        data.isExpert1=1;
        if data.Borgen==1
            Lock=[data.FixPoint1_B(1) data.FixPoint1_B(2); 0 0];
        else
            Lock=[data.FixPoint1_SV(1) data.FixPoint1_SV(2); 0 0];
        end
        
        
        dlmwrite('subfcnsBKP/wrap/LockMode.txt', Lock,'delimiter',' ');
        [fm,h]=size(FP);
        [im,h]=size(IP);
        eps_d=mima(1,2)-mima(1,1);
        if eps_d<mima(1,4)-mima(1,3)
            eps_d=mima(1,4)-mima(1,3);
        end
        for i=1:data.numb_seg
             if eps_d<mima(i,2)-mima(i,1)
                eps_d=mima(i,2)-mima(i,1);
             end
             if eps_d<mima(i,4)-mima(i,3)
                eps_d=mima(i,4)-mima(i,3);
             end
        end
        eps_d=eps_d/500;
        
        allg=[fm; im; data.on; data.epsC; data.arc_step;eps_d;data.numb_seg;data.out;data.count];
        dlmwrite('subfcnsBKP/wrap/allg.txt',allg,'delimiter',' ');
        
        
        set(Footnote,'String',' C routine in progress ... (press CTRL + C in the command window to cancel) ...','ForegroundColor','black');
        fprintf(' C routine (AFS Lock-Mode 1) in progress ... (press CTRL + C to cancel) ... ');
        set(hObject,'Enable','off');
        
        tic;
        arch = computer('arch');
        pause(0.5);
        if strcmp(arch,'maci64') == 1
            system('./subfcnsBKP/Borgen_mac64.exe ');
        elseif strcmp(arch,'win32') == 1
            system('./subfcnsBKP/Borgen_WINDOWS32.exe ');
        elseif strcmp(arch,'win64') == 1
            system('./subfcnsBKP/Borgen_WINDOWS32.exe ');
        elseif strcmp(arch,'glnx86') == 1
            system('./subfcnsBKP/Borgen_L32.exe ');
        elseif strcmp(arch,'glnxa64') == 1
            system('./subfcnsBKP/Borgen_L64.exe ');
        else
            fprintf(2,'No executable version for this type of system.\n');
            GenInfo = -2;
            AFS = [];
            totalpoin = 0;
            calls = 0;
            bisec = 0;
            parts = 0;
            dimP = 0;
            lenP = 0;
            LENP = 0;
            valid = [];
            lines = [];
            tim = 0;
            data.mima_B = 0;
            data.mima_SV = 0;
            set(gcf,'Pointer','arrow');
            cd(actpwd);
            return;
        end
        set(gcf,'Pointer','arrow');
        toc
        set(Footnote,'String',['Lock mode 1 computation-time: ' ,num2str(toc), ' seconds'],'ForegroundColor','black');
        
        Lockcomp=dlmread('subfcnsBKP/wrap/AFSLockcomp.txt');
        data.numb_segmentsL(1)=0;
        data.numb_segmentsL(2)=0;
        AFSL{1}=[];
        AFSL{2}=[];
        if Lockcomp(1,1)~=0 || Lockcomp(1,2)~=0
            
            Lock=dlmread('subfcnsBKP/wrap/AFSLock.txt');
            
            Lockout=dlmread('subfcnsBKP/wrap/AFSLockout.txt');
            k=size(Lockcomp,1);
            data.numb_segmentsL(1)=Lockcomp(1,1)+Lockcomp(1,2);
            if k>1
                data.numb_segmentsL(2)=Lockcomp(2,1)+Lockcomp(2,2)-data.numb_segmentsL(1);
            end
            if data.numb_segmentsL(1)>0
                AFSL{1}=zeros(Lockcomp(1,1)+Lockcomp(1,2),2);
                AFSL{1}(1:Lockcomp(1,1),:)=Lock(1:Lockcomp(1,1),:);
                AFSL{1}(Lockcomp(1,1)+1:Lockcomp(1,2)+Lockcomp(1,1),:)=Lockout(Lockcomp(1,2):-1:1,:);
            end
            if k>1
            if data.numb_segmentsL(2)>0
                AFSL{2}=zeros(Lockcomp(2,1)+Lockcomp(2,2)-Lockcomp(1,1)-Lockcomp(1,2),2);
                AFSL{2}(1:(Lockcomp(2,1)-Lockcomp(1,1)),:)=Lock(Lockcomp(1,1)+1:Lockcomp(2,1),:);
                AFSL{2}((Lockcomp(2,1)-Lockcomp(1,1)+1):(Lockcomp(2,2)-Lockcomp(1,2))+(Lockcomp(2,1)-Lockcomp(1,1)),:)=Lockout(Lockcomp(2,2):-1:(Lockcomp(1,2)+1),:);
            end
            end
            data.isExpert1 = 1;
            data.isExpert2 = 0;
            
            if data.Borgen==1
                data.AFSL_B=AFSL;
                
                V1=data.V_scal;
                VTV = V1'*data.Vraw;
                s1=data.sum_v1;
                s2=data.sum_v2;
                s3=data.sum_v3;
                for j=1:2
                    [m,n] = size(AFSL{j});
                    for i=1:m
                        t(2:3)=AFSL{j}(i,1:2);
                        t(1)=(1-t(2)*s2-t(3)*s3)/s1;
                        s=t*VTV;
                        s=s/s(1);
                        data.AFSL_SV{j}(i,1:2)=s(2:3);
                    end
                end
            else
                data.AFSL_SV=AFSL;
                [h,n]=size(data.Vraw);
                e=ones(h,1);
                V1=data.V_scal;
                for j=1:2
                    [m,n]=size(AFSL{j});
                    for i=1:m
                        t1=[1,AFSL{j}(i,1),AFSL{j}(i,2)];
                        h=t1*data.Vraw(:,1:3)';
                        s=h*e;
                        t2=h*V1/s;
                        data.AFSL_B{j}(i,1:2)=t2(2:3);
                    end
                end
            end
            
            set(Empty_Button,'UserData',data);
            
            set(hObject,'Enable','on');
            plotAFSLock_BKP(Empty_Button, AxesAFS, num)
            data = get(Empty_Button,'UserData');
            if data.numb_segmentsL(1)==0 || data.numb_segmentsL(2)==0
                onoffButton_BKP(LockMode,[3],[4:5]);
            else
                onoffButton_BKP(LockMode,[3 4],[5]);
            end
        end
    end
    

elseif num==2
      if (isfield(data,'FixPoint2_B') && size(data.FixPoint2_B,1)>0)
          
         set(gcf,'Pointer','Watch');

          
          if data.scalBorg==1
                FixPoint1=data.FixPoint1_B;
                FixPoint2=data.FixPoint2_B;
                AFSL=data.AFSL_B;
            else
                FixPoint1=data.FixPoint1_SV;
                FixPoint2=data.FixPoint2_SV;
                AFSL=data.AFSL_SV;
           end
   
        test=0;
        for i=1:2
            if(inpolygon(FixPoint2(1),FixPoint2(2),AFSL{i}(:,1),AFSL{i}(:,2))==1)
                test=test+1;
            end
        end
        if test==0
            data.FixPoint2_SV=[];
            data.FixPoint2_B=[];
            data.isExpert2=0;
            set(Empty_Button,'UserData',data);
            return;
        end
        
        data.isExpert2=1;
        if data.Borgen==1
            Lock=[data.FixPoint1_B(1) data.FixPoint1_B(2); data.FixPoint2_B(1) data.FixPoint2_B(2)];
        else
            Lock=[data.FixPoint1_SV(1) data.FixPoint1_SV(2); data.FixPoint2_SV(1) data.FixPoint2_SV(2)];
        end
        dlmwrite('subfcnsBKP/wrap/LockMode.txt', Lock,'delimiter',' ');
        
        
        set(Footnote,'String',' C routine in progress ... (press CTRL + C in the command window to cancel) ...','ForegroundColor','black');
        fprintf(' C routine (AFS Lock-Mode 2) in progress ... (press CTRL + C to cancel) ... ');
        set(hObject,'Enable','off');
        
        tic
        arch = computer('arch');
        pause(0.5);
        if strcmp(arch,'maci64') == 1
            system('./subfcnsBKP/Borgen_mac64.exe ');
        elseif strcmp(arch,'win32') == 1
            system('./subfcnsBKP/Borgen_WINDOWS32.exe ');
        elseif strcmp(arch,'win64') == 1
            system('./subfcnsBKP/Borgen_WINDOWS32.exe ');
        elseif strcmp(arch,'glnx86') == 1
            system('./subfcnsBKP/Borgen_L32.exe ');
        elseif strcmp(arch,'glnxa64') == 1
            system('./subfcnsBKP/Borgen_L64.exe ');
        else
            fprintf(2,'No executable version for this type of system.\n');
            GenInfo = -2;
            AFS = [];
            totalpoin = 0;
            calls = 0;
            bisec = 0;
            parts = 0;
            dimP = 0;
            lenP = 0;
            LENP = 0;
            valid = [];
            lines = [];
            tim = 0;
            data.mima_B = 0;
            data.mima_SV=0;
            set(gcf,'Pointer','arrow');
            cd(actpwd);
            return;
        end
        set(gcf,'Pointer','arrow');
        toc
        set(Footnote,'String',['Lock mode 2 computation-time: ' ,num2str(toc), ' seconds'],'ForegroundColor','black');
        
        set(hObject,'Enable','on');
        Lockcomp=dlmread('subfcnsBKP/wrap/AFSLockcomp.txt');
        
        data.numb_segmentsL2=0;
        AFSL2=[];
        if Lockcomp(1,1)~=0
            Lock=dlmread('subfcnsBKP/wrap/AFSLock.txt');
            data.numb_segmentsL2=Lockcomp(1,1);
            AFSL2=zeros(Lockcomp(1,1),2);
            AFSL2(1:Lockcomp(1,1),:)=Lock(1:Lockcomp(1,1),:);
            
            
            if data.Borgen==1
                data.AFSL2_B=AFSL2;
                
                V1=data.V_scal;
                VTV = V1'*data.Vraw;
                s1=data.sum_v1;
                s2=data.sum_v2;
                s3=data.sum_v3;
                
                    [m,n] = size(AFSL2);
                    for i=1:m
                        t(2:3)=AFSL2(i,1:2);
                        t(1)=(1-t(2)*s2-t(3)*s3)/s1;
                        s=t*VTV;
                        s=s/s(1);
                        data.AFSL2_SV(i,1:2)=s(2:3);
                    end
                
            else
                data.AFSL2_SV=AFSL2;
                [h,n]=size(data.Vraw);
                e=ones(h,1);
                V1=data.V_scal;
                
                    [m,n]=size(AFSL2);
                    for i=1:m
                        t1=[1,AFSL2(i,1),AFSL2(i,2)];
                        h=t1*data.Vraw(:,1:3)';
                        s=h*e;
                        t2=h*V1/s;
                        data.AFSL2_B(i,1:2)=t2(2:3);
                    end
                
            end
            
            set(Empty_Button,'UserData',data);
            data.isExpert1 = 1;
            data.isExpert2 = 1;
            
            plotAFSLock_BKP(Empty_Button, AxesAFS,num)
            data = get(Empty_Button,'UserData');
            
        end
        if data.numb_segmentsL2==0
            onoffButton_BKP(LockMode,[5],[]);
        else
            onoffButton_BKP(LockMode,[5],[]);
        end
        
        
    end
end
cd(actpwd);
set(Empty_Button,'UserData',data);
