Agnieszka
Agnieszka

Reputation: 61

Matlab GUI, how to go back from subplot to a single plot?

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);

Upvotes: 3

Views: 762

Answers (1)

Suever
Suever

Reputation: 65430

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)

Upvotes: 3

Related Questions