When calling plot or axes you can optionally specify a "parent" or "container", where the axes or plot object gets created in. The container is the handle object to the object where the new object will be created in. If you don't specify the container, it will be created in the "current figure" or "current axes". This can be literally anything you created or selected with your mouse before.
If you call plot and no current axes or figure exists, it will create the missing figure window, create an axes object inside it and then creates the plot into the axes.
See this example which creates some figures and uses a button in one window to plot something in another window (also attached):
function example_436887
h_fig1 = figure;
h_fig2 = figure;
h_fig3 = figure;
h_ax2 = axes(h_fig2);
h_ax3 = axes(h_fig3);
plot(h_ax2,1:10,rand(1,10))
h_fig1.Position(3) = 350;
h_fig1.Position(4) = 100;
set(h_fig1,'MenuBar','none',...
'ToolBar','none');
button_plot = uicontrol(h_fig1, ...
'Style','pushbutton',...
'String','Call Plot in Figure specified as h_fig3', ...
'Position',[20,20,300,25],...
'Callback',@button_plot_Callback);
function button_plot_Callback(source,eventdata)
plot(h_ax3,1:10,rand(1,10));
end
end
When pushing the button to plot inside h_fig1, the target container needs to be specified. If you change the Callback-Function from this:
function button_plot_Callback(source,eventdata)
plot(h_ax3,1:10,rand(1,10));
end
to this:
function button_plot_Callback(source,eventdata)
plot(1:10,rand(1,10));
end
The error you described occurs, as the plot command creates an axes in the current figure (which is the figure with the button) because there is no axes to plot into.
Best Answer