3

I am having a simple GUI with two pushbuttons. One of them is plotting a single plot, one is plotting two subplots. However, once I push the subplot option, I cannot go back to the single plot. I am getting an error:

error using axes, invalid object handle

Please see below my very simple example:

function plot_push1_Callback(hObject, eventdata, handles)
load('test.mat')
axes(handles.axes1)
cla(handles.axes1,'reset')
plot(x,x.^(n+1));

function push_plot2_Callback(hObject, eventdata, handles)
load('test.mat')
axes(handles.axes1)
cla(handles.axes1,'reset')
subplot(2,1,1);
plot(x,x.^(0));
subplot(2,1,2);
plot(x,x);
Mario Boss
  • 1,784
  • 3
  • 20
  • 43
Agnieszka
  • 61
  • 3

1 Answers1

3

The main issue here is that subplot creates a new axes object (or transforms the current axes). You'll need to take this into consideration when manipulating your axes objects.

axes(handles.axes1);    

subplot(2,1,1);             % This is still handles.axes1
plot(x, x.^(0))

newax = subplot(2,1,2);     % This is a new axes
plot(x, x);

If you want to use a container in GUIDE, I would define a uipanel instead of an axes. Then all subplots can live within this panel.

function plot_push1_callback(hObject, eventdata, handles)
    % Make one plot in the panel
    subplot(1,1,1, 'Parent', handles.panel);

    plot(x, x.^(n+1));

function plot_push2_callback(hObject, eventdata, handles)

    % Make the first subplot in the panel
    subplot(2,1,1, 'Parent', handles.panel)

    plot(x, x.^0);

    % Make the second subplot in the panel
    subplot(2,1,2, 'Parent', handles.panel)

    plot(x, x)
Suever
  • 64,497
  • 14
  • 82
  • 101
  • In Guide I have prepared a dedicated space (axes1) in which I would like to plot different things, depending on the pushbutton pressed. I have tried to put the suggested `delete()` instead of my `cla()` but it also doesn't work. I am a bit lost now, how do you suggest I should arrange these two pushbuttons? – Agnieszka Jun 16 '16 at 14:06
  • @Agnieszka A subplot creates two axes. In GUIDE you would want to use a `uipanel` as the container to plot things in NOT an axes. – Suever Jun 16 '16 at 14:06
  • @Agnieszka Added an example of how to do that – Suever Jun 16 '16 at 14:09