function buttonautocomputeAPGA(hObject, eventdata, Empty_Button, Buttons, RegSlider, RegText, AxesSOL, AxesAOM, calcp, usmn, usmf, lcs, AxesD, AxesSVD, LCS, rbplotsvd, Recondition, levpeakmark)
data = get(Empty_Button,'UserData');

data.isautosol = 0;
data.isred = 0;
data.issol = 0;
data.islive = 0;
data.livet = [];
set(usmn,'Value',0);
set(usmf,'Value',1);
SliderOnOffPGA(RegSlider, RegText, data, 0);
set(Empty_Button,'UserData',data);

if (isfield(data,'isdata')*isfield(data,'issvd')*isfield(data,'isnovctrs')*isfield(data,'isautodetect') == 1) && (data.isdata*data.issvd*data.isnovctrs*data.isautodetect == 1)
    final = data.autodetect;

    % sel = 1;
    % lev = 0.025;
    % lev = 0.01;
    % 
    % ind = find(VAL(sel,:)>lev);
    % 
    % new = ind;
    % final = [];
    % while ~isempty(new)
    %     i = 1;
    %     while i<length(new) && new(i+1)==new(i)+1
    %         i = i+1;
    %     end
    %     in = new(1:i);
    %     [~,id] = max(VAL(sel,in));
    %     final(end+1) = in(id);
    %     if length(new) < i+1
    %         break
    %     end
    %     new = new(i+1:end);
    % end

    [k,n] = size(data.D);
    % ifir = data.ifir;
    % ilas = data.ilas;

    isgs = data.isgivenspec;
    
    givenadaptedis = data.givenadaptedis;

    if isfield(data,'noc') == 1
        noc = data.noc;
        A = [];
        for i=1:data.noc
            A = [A; data.SOL(i).a];
        end
    else
        noc = 0;
        A = 0;
    end

    z   = data.novctrs;
    L   = data.U*data.S(:,1:z);
    S   = diag(data.S(1:z,1:z));
    R   = data.V(:,1:z)';

    amin = -getsetvar(RegText(63), 0, 0.1, 0, 2);

    nw = 6;
    for i=1:nw
        W(i) = get(RegSlider(3+i), 'Value');
        if (i>1) && (abs(W(i)-get(RegSlider(3+i), 'Min')) < 1.E-14)
            W(i) = -1;
        end
    end

    isred = 0;
    for i=2:z
        mi = min(data.V(:,i));
        if (mi > 0) || (-mi < 1.E-16*max(data.V(:,i)))
            isred = 1;
            break;
        end
    end
    
    actpwd = pwd;
    cd(calcp);
    
    dlmwrite('wrap/L.txt',L,'precision','%15.15e','delimiter',' ');
    dlmwrite('wrap/R.txt',R,'precision','%15.15e','delimiter',' ');
    dlmwrite('wrap/S.txt',S,'precision','%15.15e','delimiter',' ');
    dlmwrite('wrap/W.txt',W,'precision','%15.15e','delimiter',' ');
    dlmwrite('wrap/A.txt',A,'precision','%15.15e','delimiter',' ');

    if isgs && get(RegSlider(8),'Value') > 0
        if givenadaptedis == 1
            dlmwrite('wrap/givena.txt',data.givenadaptedI','precision','%d');
            dlmwrite('wrap/givena.txt',data.givena,'-append','precision','%e');
        else
            givena = data.givena;
            dlmwrite('wrap/givena.txt',givena','precision','%15.15e','delimiter',' ');
        end 
    end
    data.tinter = [];
    for ijk=1:length(final)
        ifir = final(ijk);
        ilas = final(ijk);

        if isred == 0
            inot = setdiff(1:n, ifir:ilas);
            t0 = -(R(2:z,inot)')\(R(1,inot)');
        else
            t0 = 0.1*ones(z,1);
        end     

        dlmwrite('wrap/allg.txt',[k; n; z; nw; ifir; ilas; isgs; isred; noc; givenadaptedis],'precision','%d');
        dlmwrite('wrap/allg.txt',amin,'-append','precision','%e');
        dlmwrite('wrap/t0.txt',t0,'precision','%15.15e','delimiter',' ');
        dlmwrite('wrap/tim.txt',0,'precision','%d');

        arch = computer('arch');
        opt = 1;

        if (strcmp(arch,'maci64') == 1) || (strcmp(arch,'maca64') == 1)
            system(['./PeakGroupAnalysisM64.exe ',num2str(opt),' 0']);
        elseif (strcmp(arch,'win64') == 1) || (strcmp(arch,'win32') == 1)
            system(['PeakGroupAnalysisW32.exe ',num2str(opt),' 0']);
        elseif strcmp(arch,'glnx86') == 1
            system(['./PeakGroupAnalysisU32.exe ',num2str(opt),' 0']);
        elseif strcmp(arch,'glnxa64') == 1
            system(['./PeakGroupAnalysisU64.exe ',num2str(opt),' 0']);
        else
            fprintf(2,'No executable version for this type of system.\n');
            cd(actpwd);
            return;
        end
        tim = [];
        tim = load('wrap/tim.txt');

        if tim(1) < 1
            fprintf('computation canceled.\n');
            return;
        else
            tim = tim(2);
        end
        results = load('wrap/results.txt');
        t = results(1:z)';
        data.yinter(ijk,:) = results(z+1:z+nw)';
        a = t*R;
        data.ainter(ijk,:) = a/max(a);
        data.tinter(ijk,:) = t;
        data.W(ijk,:) = W;
        vs = R(:,ifir:ilas)*a(ifir:ilas)'/(a(ifir:ilas)*a(ifir:ilas)');

        autodetect{ijk} = detectallpeaks(data.x,a,levpeakmark,ifir,ilas);
    end
    cd(actpwd);
    data.final = final;
    
    h = finalcheck(hObject, eventdata, Empty_Button, Buttons, data, lcs, autodetect);
    uiwait(h);

    data = get(Empty_Button,'UserData');
    if data.isautosol == 0
        return
    end
    
    mid = min(0,min(min(data.D)));
    mad = max(max(data.D))*1.1;
    nc = size(lcs,1);
    lgntxt = {};
    for i=1:length(data.final)
        plot(AxesSOL, data.x(final(i))*[1 1], [mid mad], 'color',lcs(mod(i-1,nc)+1,:),'Linewidth',1.5);
        hold(AxesSOL,'on');
        lgntxt{i} = num2str(round(10*data.x(data.final(i)))/10);
    end
    for i=1:length(data.final)
        plot(AxesSOL, data.x, data.ainter(i,:), 'color',lcs(mod(i-1,nc)+1,:), 'Linewidth',2);
    end
    hold(AxesSOL,'off');
    axis(AxesSOL,'tight');
    set(AxesSOL,'ylim',[mid mad],'ytick',[]);
    xlabel(AxesSOL,'channel');
    lgn = legend(lgntxt);
    set(lgn,'fontsize',12);
    
    if size(data.ainter,1)>0
        plot(AxesAOM(1), data.x, data.ainter, 'k', 'Linewidth',2);
        axis(AxesAOM(1),'tight');
        yl = get(AxesAOM(1),'ylim');
        yl = [min(0,yl(1)), yl(2)*1.1];
        set(AxesAOM(1),'ylim',yl, 'XTick', [], 'YTick', []);
    end
    onoffButton(Buttons,[12 13:14 16],[5 6 17]);
    set(Buttons(6),'BackgroundColor',0.94*[1 1 1]);
    set(Buttons(17),'BackgroundColor',0.94*[1 1 1]);

    data.new = length(data.final);
    data.isautosol = 1;
    
    data.isautodetect = 0;
    set(Empty_Button,'UserData',data);
    
    buttonacceptcurrentaPGA(hObject, eventdata, Empty_Button, Buttons, AxesD, AxesSVD, AxesSOL, AxesAOM, lcs, LCS, rbplotsvd, Recondition, RegSlider, RegText, usmn, usmf);
end
