vram
vram

Reputation: 103

How to delete multiple plot axes made with subplot on scilab

how are you. I've all day trying to do the following: I'm designing a PID controller and depending on how much data I want to analize then I show only one plot or four plots in the same figure (this last multiple plot is made with subplot).

All the calculation and plots are made in one function with options in function dependind on what I want to see, I have several buttons and text inputs in the application that I'm coding and right know the PID design and the simple or multiple plots are working; my current program is that the axes handle is being assigned to local variables inside the function and of course this variables are distroyed when the program exits the function.

A more detailed explanation of the problem is the following: if I select multiple plot and then I want one plot then I don't know how to delete the four axes to clean the figure without closing it, so when I want to activate the simple plot then the program only erases one of the four plots and prints the simple plot over the remaining three while the remaining three subplots remains visible. Following is the piece of code that is giving me problems:

grafico=sistem_graf;
disp(grafico);
if(isdef('aa')==%T);
    scf(aa);
    delete(get("current_axes"));
    disp("aa");
end
if(isdef('bb')==%T);
    scf(bb);
    delete(get("current_axes"));
    disp("bb");
end
if(isdef('cc')==%T);
    scf(cc);
    delete(get("current_axes"));
    disp("cc");
end
if(isdef('dd')==%T);
    scf(dd);
    delete(get("current_axes"));
    disp("dd");
end

select ventana
case 0
    tipoG=0;
    plot2d(T,graffta);
    xtitle('Ω(S)/R(S): Respuesta al escalón (Sistema original)','t [s]','Vel [rpm]'); 
    legend(['Original';'C/control P';'C/Control PI';'C/Control PID']);
    aa=get("current_axes");
    aa.axes_bounds = [1/3 0 2/3 1];
case 1
    tipoG=1;        
//        delete(get("current_axes"));        
    plot(T,[graffta;sist_P_ZN;sist_PI_ZN;sist_PID_ZN]);
    xtitle('Ω(S)/R(S): Respuesta al escalón','t [s]','Vel [rpm]'); 
    legend(['Original';'C/control P';'C/Control PI';'C/Control PID']);
    aa=get("current_axes");
    aa.axes_bounds=[1/3 0 2/3 1];
case 2
    tipoG=2;        
    //delete(get("current_axes"));  
    subplot(421);
    plot(T,[graffta;sist_P_ZN;sist_PI_ZN;sist_PID_ZN]);
    xtitle('Ω(S)/R(S): Respuesta al escalón','t [s]','Vel [rpm]'); 
    legend(['Original';'C/control P';'C/Control PI';'C/Control PID']);
    aa=get("current_axes");
    aa.axes_bounds=[1/3,0,1/3,1/2]
    subplot(422);
    plot2d(T,(kt*kp/(j*la))*(1/(afta*bfta))*(1+(1/(afta-bfta))*(bfta*exp(-afta*T)-afta*exp(-bfta*T))));
    xtitle('Ω(t)','t [s]','Vel [rpm]');
    bb=get("current_axes");
    bb.axes_bounds=[2/3,0,1/3,1/2];
    subplot(423);
    plot2d(T,(kt/(j*la))*(1/(afta*bfta))*((1/(afta-bfta))*(bfta*afta*exp(-bfta*T)-afta*bfta*exp(-afta*T))));
    xtitle('Variación de velocidad angular dΩ/dt','t [s]','Ac. [rpm/s^2]');
    cc=get("current_axes");
    cc.axes_bounds=[1/3,1/2,1/3,1/2];
    subplot(424);
    plot(T,[sist_P_ZN;sist_PI_ZN;sist_PID_ZN]);
    xtitle('Controladores','t [s]','Vel [rpm]');
    legend(['C/control P';'C/Control PI';'C/Control PID']);
    dd=get("current_axes");
    dd.axes_bounds=[2/3,1/2,1/3,1/2];
end

the four if(isdef("variable_name")) never doesn't works because when the program enters the functions those variables doesn't exists yet, those are created after the plots with subplot.

If the graphics properties are displayed from the Edit menú of the figure then all the children axes are shown but if I try to get the axes with for example

a=get(sistem_graf.Axes(1))

then a console display is shown saying that the property "Axes" doesn't exist so I don't know how to solve the problem.

Please help!

Thanks in advance.

Update 03/06/2021:

I found a way to have the behavior I need an is working at 90%, it only fails when I go from detailed plot to simple plot (is erasing one graphic) but backwards it works fine, I changed all the code before "Select ventana" to the following

if(imps(1) ~= -1)
    if(size(imps) == 1)
        sca(imps(1));
        disp(imps(1));
        delete(get("current_axes"));
    elseif(size(imps) > 1)
        sca(imps(1));
        delete(get("current_axes"));
        sca(imps(2));
        delete(get("current_axes"));
        sca(imps(3));
        delete(get("current_axes"));
        sca(imps(4));
        delete(get("current_axes"));
    end
end

and filled an array whose content is all the axes ploted last time that the function was called, that array is returned from the function to a global variable and finally that global variable is feed to the function in the next call and assigned to the local variable "imps".

I think is some kind of crazy but apparently scilab doesn't have incorpotated the static variable aproach.

Upvotes: 1

Views: 314

Answers (1)

Stéphane Mottelet
Stéphane Mottelet

Reputation: 3014

If I have understood your problem, the small example below should help you:

function fun(nb)
    ch = gcf().Children;
    delete(ch(ch.type=="Axes"))
    t = linspace(0,1,100);
    if nb==1
        plot(t,t)
    else
        for i=1:nb
            subplot(2,2,i)
            plot(t,t.^i)
        end
    end
endfunction
clf
uicontrol("style","pushbutton","units","normalized","Position",[0.1 0.1 0.4 0.1],"string","one plot","callback","fun(1)");
uicontrol("style","pushbutton","units","normalized","Position",[0.5 0.1 0.4 0.1],"string","four plots","callback","fun(4)");

I think that the mechanism you were missing is the following:

ch = gcf().Children;
delete(ch(ch.type=="Axes"))

i.e. something that allows to delete the plot axes and keep the uicontrols alive. The above construct uses logical indexing. Here ch.type=="Axes" is a vector of boolean values with components i egal to true when the type field of corresponding component ch(i) is Axes. Then, ch(ch.type=="Axes") is the subset of components of ch such that the boolean index has value true. Hence, at last, the subset of components of ch which are of Axes type.

Upvotes: 2

Related Questions