function button_save(hObject, eventdata, parentfig,Empty_Button,Footnote,Plotsettings,borg)

data = get(Empty_Button,'UserData');

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



if borg==1
    
    
    U = data.U_scal;
    S = data.S_scal;
    V = data.V_scal;
    D = data.D_scal;
else
    U=data.Uraw(:,1:3);
    S=data.Sraw;
    V=data.Vraw(:,1:3);
    D=data.Dtrunc;
end



if borg==1
    AFS = data.segments_B;
    IP = data.IP_B;
    FP = data.FP_B;
else
    AFS=data.segments_SV;
    IP = data.IP_SV;
    FP = data.FP_SV;
end



bndpts = getsetvar_BKP(Plotsettings(1), 1, 200, 1, 1);
hx     = getsetvar_BKP(Plotsettings(2), 5.E-3, 5.E-1, 0, 1);
hy     = getsetvar_BKP(Plotsettings(3), 5.E-3, 5.E-1, 0, 1);


if data.numb_seg == 0
    printf('There is no AFS to save.\n');
    TransformedAFS = [];
elseif data.numb_seg == 1

    Sol= transformAFS_BKP(data,1,1,bndpts,hx,hy);
  
    TransformedAFS{1} = Sol.A;
else
    for part=1:data.numb_seg
        Sol = transformAFS_BKP(data,part,data.numb_seg,bndpts,hx,hy);
        TransformedAFS{part} = Sol.A;

    end
end



if (isfield(data,'FixPoint3_B') == 1) && (size(data.FixPoint3_B,1) > 0)
    if borg==1
        AFSLockMode1 = data.AFSL_B;
        LockPoint1 = data.FixPoint1_B;
    else
        AFSLockMode1 = data.AFSL_SV;
        LockPoint1 = data.FixPoint1_SV;
    end
        
    if borg==1
        n=size(V,1);
        e=ones(n,1);
        s1=V(:,1)'*e;
        s2=V(:,2)'*e;
        s3=V(:,3)'*e;
        t1=(1-data.FixPoint1_B(1)*s2-data.FixPoint1_B(2)*s3)/s1;
        ALock1 = [t1 data.FixPoint1_B]*V';
    else

        ALock1 = [1 data.FixPoint1_SV]*V';
    end
    if borg==1
        AFSLockMode2 = data.AFSL2_B;
        LockPoint2 = data.FixPoint2_B;
        LockPoint3 = data.FixPoint3_B;
    else
        AFSLockMode2 = data.AFSL2_SV;
        LockPoint2 = data.FixPoint2_SV;
        LockPoint3 = data.FixPoint3_SV;
    end
    if borg==1
        t2=(1-data.FixPoint2_B(1)*s2-data.FixPoint2_B(2)*s3)/s1;
        ALock2 = [t2 data.FixPoint2_B]*V';
        t3=(1-LockPoint3(1)*s2-LockPoint3(2)*s3)/s1;
        ALock3 = [t3 LockPoint3]*V';
        T(:,1)=[t1 t2 t3]';
        T(1,2:3)=data.FixPoint1_B(1:2);
        T(2,2:3)=data.FixPoint2_B(1:2);
        T(3,2:3)=LockPoint3(1:2);
        
    else
        ALock2 = [1 data.FixPoint2_SV]*V';
        ALock3 = [1 LockPoint3]*V';
        T(:,1)=[1 1 1]';
        T(1,2:3)=data.FixPoint1_SV(1:2);
        T(2,2:3)=data.FixPoint2_SV(1:2);
        T(3,2:3)=LockPoint3(1:2);
    end
    C=U*S*inv(T);
    A=T*V';
    if borg==1
        uisave({'A','C','U','S','V','D','AFS','IP','FP','TransformedAFS','AFSLockMode1','LockPoint1','ALock1','AFSLockMode2','LockPoint2','ALock2', 'LockPoint3', 'ALock3'},...
        [data.PathName,'results_RowSumScal_',data.FileName]);
    else
          uisave({'A','C','U','S','V','D','AFS','IP','FP','TransformedAFS','AFSLockMode1','LockPoint1','ALock1','AFSLockMode2','LockPoint2','ALock2','LockPoint3', 'ALock3'},...
        [data.PathName,'results_firstSVscal_',data.FileName]);
    end
    
elseif (isfield(data,'FixPoint2_B') == 1) && (size(data.FixPoint2_B,1) > 0)
    if borg==1
        AFSLockMode1 = data.AFSL_B; 
        LockPoint1 = data.FixPoint1_B;
    else
        AFSLockMode1 = data.AFSL_SV;
        LockPoint1 = data.FixPoint1_SV;

    end
   
    if borg==1
        n=size(V,1);
        e=ones(n,1);
        s1=V(:,1)'*e;
        s2=V(:,2)'*e;
        s3=V(:,3)'*e;
        t1=(1-data.FixPoint1_B(1)*s2-data.FixPoint1_B(2)*s3)/s1;
        ALock1 = [t1 data.FixPoint1_B]*V';
    else
        ALock1 = [1 data.FixPoint1_SV]*V';
    end
    if borg==1
        AFSLockMode2 = data.AFSL2_B;
        LockPoint2 = data.FixPoint2_B;
    else
        AFSLockMode2 = data.AFSL2_SV;
        LockPoint2 = data.FixPoint2_SV;
    end
    
    if borg==1
        t2=(1-data.FixPoint2_B(1)*s2-data.FixPoint2_B(2)*s3)/s1;
        ALock2 = [t2 data.FixPoint2_B]*V';
    else
        ALock2 = [1 data.FixPoint2_SV]*V';
    end
    if borg==1
        uisave({'U','S','V','D','AFS','IP','FP','TransformedAFS','AFSLockMode1','LockPoint1','ALock1','AFSLockMode2','LockPoint2','ALock2'},...
                [data.PathName,'results_RowSumScal_',data.FileName]);
    else
         uisave({'U','S','V','D','AFS','IP','FP','TransformedAFS','AFSLockMode1','LockPoint1','ALock1','AFSLockMode2','LockPoint2','ALock2'},...
                [data.PathName,'results_firstSVscal_',data.FileName]);
    end
elseif (isfield(data,'FixPoint1_B') == 1) && (size(data.FixPoint1_B,1) > 0)
    if borg==1
        AFSLockMode1 = data.AFSL_B;
        LockPoint1 = data.FixPoint1_B;
    else
        AFSLockMode1 = data.AFSL_SV;
        LockPoint1 = data.FixPoint1_SV;
    end
    if borg==1
        n=size(V,1);
        e=ones(n,1);
        s1=V(:,1)'*e;
        s2=V(:,2)'*e;
        s3=V(:,3)'*e;
        t1=(1-data.FixPoint1_B(1)*s2-data.FixPoint1_B(2)*s3)/s1;
        ALock1 = [t1 data.FixPoint1_B]*V';
    else
        ALock1 = [1 data.FixPoint1_SV]*V';
    end
    if borg==1
        
        uisave({'U','S','V','D','AFS','IP','FP','TransformedAFS','AFSLockMode1','LockPoint1','ALock1'},[data.PathName,'results_RowSumScal_',data.FileName]);
    else
        uisave({'U','S','V','D','AFS','IP','FP','TransformedAFS','AFSLockMode1','LockPoint1','ALock1'},[data.PathName,'results_firstSVscal_',data.FileName]);
    end
else
    if borg==1
        uisave({'U','S','V','D','AFS','IP','FP','TransformedAFS'},[data.PathName,'results_RowSumScal_',data.FileName]);
    else
        uisave({'U','S','V','D','AFS','IP','FP','TransformedAFS'},[data.PathName,'results_firstSVscal_',data.FileName]);
    end
end