function buttonWFAPGA(hObject, eventdata, Empty_Button, Buttons, lcs, basichi, basicfsz, sty, RegSlider, RegText, usmn, usmf)
data = get(Empty_Button,'UserData');

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,'isbd') == 1) && (data.isdata*data.isbd == 1)
    fig = figure(11);
    pause(.1);
    
    scrsz = get(0,'ScreenSize');
    
    set(fig,'Position',[scrsz(3)/4 scrsz(4)/4 scrsz(3)/2 scrsz(4)/2]);
    pause(.1);
    
    set(fig,'Name','Evolving factor analysis for the selected channels window','NumberTitle','off','MenuBar','none','Toolbar','none','color','white');
    
    Textfield(1) = uicontrol('Style','text','String','Evolving factor analysis in progress: ','HorizontalAlignment','left',...
        'Fontsize',2*basicfsz,'BackgroundColor','white','Units','normalized','FontName',sty,'Position',[0.15, 0.91, 0.7, 2*basichi]);
    
    AxesEFA = axes('Fontsize',2*basicfsz,'Units','normalized','FontName',sty,'Position',[0.1, 0.1, 0.8, 0.8]);
    
    innerEFA(data, Textfield, AxesEFA);
    
else
    return
end




function innerEFA(data, Textfield, AxesEFA)
WD = data.D(:,data.ifir:data.ilas);

[k,n] = size(WD);

ne = 10;
ne = min([ne, k, n]);
SF = zeros(ne,k);
SB = zeros(ne,k);

st = .01;
pr = 0;

for i=1:k
    S = svds(WD(1:i,:),ne);
    SF(1:min([ne i length(S)]),i) = S;
    
    S = svds(WD(i:k,:),ne);
    SB(1:min([ne k-i+1 length(S)]),i) = S;
    
    if i/k > 0.999*(pr+st)
        pr = st+pr;
        set(Textfield,'String',['Evolving factor analysis for the channels window in progress: ',num2str(100*pr),'% ready']);
    end
    pause(0.001);
end

semilogy(AxesEFA, data.time, SF(1,:), 'b', data.time, SB(1,:), 'r', 'Linewidth',2);
hold(AxesEFA,'on')
semilogy(AxesEFA, data.time, SF(2:end,:), 'b', data.time, SB(2:end,:), 'r', 'Linewidth',2);
hold(AxesEFA,'off')

set(AxesEFA,'Xlim',[min(data.time) max(data.time)],'Ylim',[0.9 1.1].*[min(min(min(SF)), min(min(SB))) max(max(max(SF)), max(max(SB)))]);
xlabel('time');
ylabel('singular value')
legend('forward EFA','backward EFA')

set(Textfield,'String','Evolving factor analysis for the channels window','HorizontalAlignment','center');



function innerbuttondelete(hobject, eventdata, Empty_Button, Buttons, DataPopup, AxesSOL, AxesAOM, RegSlider, RegText, lcs, parentfig)
data = get(Empty_Button,'UserData');
nod = get(DataPopup(1), 'Value');

data.iscon = 0;

if data.noc == 1
    data.isSOL = 0;
    data.noc = 0;
    data.SOL = [];
    
    set(RegSlider(9), 'Enable', 'off');
    set(RegText(9), 'Enable', 'off');
    set(RegText(29), 'Enable', 'off');
    
    onoffButton(Buttons,[],[7:10]);
    
    plot(AxesSOL,0,0,'w');
    set(AxesSOL, 'XTick', [], 'YTick', []);
    
    plot(AxesAOM(2),0,0,'w');
    set(AxesAOM(2), 'XTick', [], 'YTick', []);
    
    plot(AxesAOM(3),0,0,'w');
    set(AxesAOM(3), 'XTick', [], 'YTick', []);
else
    data.SOL = data.SOL([1:nod-1 nod+1:end]);
    s = data.noc-1;
    data.noc = s;
    
    for i=1:s
        plot(AxesAOM(2), data.x, data.SOL(i).a, 'color', lcs(mod(i-1,size(lcs,1))+1,:),'Linewidth',1);
        hold(AxesAOM(2), 'on');
    end
    hold(AxesAOM(2), 'off');
    axis(AxesAOM(2),'tight');
    yl = get(AxesAOM(2),'ylim');
    yl = [min(0,yl(1)), yl(2)*1.1];
    set(AxesAOM(2),'ylim',yl, 'XTick', [], 'YTick', []);
    
    lc = [];
    lcf = [];
    yl = [inf, -inf];
    for i=1:s
        lc(i,:) = lcs(mod(i-1,size(lcs,1))+1,:);
        lcf(i,:) = (lc(i,:) > 0)+.8*(lc(i,:) == 0);
        
        yl = [min(yl(1), min(data.SOL(i).a)), max(yl(2), max(data.SOL(i).a))];
    end
    yl = [min(0,yl(1)), yl(2)*1.1];
    
    axes(AxesSOL);
    for i=1:s
        fill(data.SOL(i).xbd([1 2 2 1 1]), yl([1 1 2 2 1]), lcf(i,:));
        hold(AxesSOL, 'on');
    end
    for i=1:s
        plot(AxesSOL, data.x, data.SOL(i).a, 'color', lc(i,:),'Linewidth',2);
    end
    hold(AxesSOL, 'off');
    set(AxesSOL, 'Xlim', [min(data.x), max(data.x)], 'ylim',yl);
    xlabel(AxesSOL,'channel');
    title(AxesSOL, 'All spectra');
end
set(Empty_Button,'UserData',data);
close(parentfig);





function innerbuttoncancel(hobject, eventdata, parentfig)
close(parentfig);
