1
votes

I created a GUI program with MATLAB, with a menu bar and tabs, each containing a plot, a text box, etc. The problem is that when I select a tab from the menu bar and plot something, the axes objects from former plots don't disappear.

I tried to use cla reset unsuccessfully. clf worked, but my menu bar disappeared as well.

Here is my code:

function fel1_Callback(hObject, eventdata, handles) %% plot sin(x)    
    cla reset
    clc
    clear all

    d = inputdlg('n:','Ertekadas',1);
    n = str2double(d);

    x=linspace(-3*pi,3*pi,1000);
    y=sin(x);
    plot(x,y,'k','LineWidth',4)
    sz='ymcrgbkymcrgbkymcrgbkymcrgbk';
    hold on
    title('Sin(x) Taylor sora')
    %n = str2num(N);
    f=zeros(size(x));
    for i=1:n
        t=(-1)^(i-1)*x.^(2*i-1)/factorial(2*i-1);
        f=f+t;
        plot(x,f,sz(i),'LineWidth',2)
        axis([-10 10 -10 10])
        pause(0.1)
        hold on
        n=n+1;
    end

function fel7_Callback(hObject, eventdata, handles) %%Sum 1/n^2
    clear all
    clc
    cla reset

    title('Suma 1/n^2','fontsize',20)
    d = inputdlg('Epszilon:','Ertek',1);
    epsz = str2double(d);
    n=1;
    x=0;
    while 1/n^2>epsz
        x=x+sum(1/n^2);
        n=n+1;
    end

    A = uicontrol('style','text','units','pixels',...
        'position',[550 550 120 40],'fontsize',20,'string','Epsz =');
    B = uicontrol('style','text','units','pixels',...
        'position',[670 550 120 40],'fontsize',20);
    set(B,'String',epsz)
    C = uicontrol('style','text','units','pixels', ...
        'position',[550 400 120 40],'fontsize',20,'string','Osszeg =');
    D = uicontrol('style','text','units','pixels',...
        'position',[670 400 120 40],'fontsize',20);
    set(D,'String',x)

I use only one main GUI figure. My menu bar contains a lot of plots and calculations, not only these two.

1

1 Answers

0
votes

The problem is that hold on prevents anything from being erased and just keeps adding to the plot. Nowhere in your code do you turn hold off. If you want to keep using the hold command, your code needs to look like this:

function fel1_Callback(hObject, eventdata, handles) %% plot sin(x)    

d = inputdlg('n:','Ertekadas',1);
n = str2double(d);

x=linspace(-3*pi,3*pi,1000);
y=sin(x);
hold off % The next plot command should now clear the old plot and create a new one**
plot(x,y,'k','LineWidth',4)
sz='ymcrgbkymcrgbkymcrgbkymcrgbk';
hold on
title('Sin(x) Taylor sora')
%n = str2num(N);
f=zeros(size(x));
for i=1:n
    t=(-1)^(i-1)*x.^(2*i-1)/factorial(2*i-1);
    f=f+t;
    plot(x,f,sz(i),'LineWidth',2)
    axis([-10 10 -10 10])
    pause(0.1)
    %hold on  %not necessary, this was turned on before the loop
    n=n+1;
end
hold off  % return the figure to the normal (default) "hold off" state