Problem with the main axes object being removed on GUIDE when using subplots

2 views (last 30 days)
I have an axes object placed in a gui in Matlab where I make some subplots. The problem is that I can access the handle of the axes the first time but after I make a subplot on it main axes is removed so I have no way to access it as the subplots are created within in removing the main object.
I want to make some subplots in a figure in the gui and then make some others subplots but since the main axes object is lost there is no way to do this. I could technically remove all the subplots but one and it may work but I don't like it. Alternativately maybe I can remove all the objects within the panel and creating a new axes using code.
Both solutions seem too complicated for something that should be straightforward. Why there's no figure object in the matlab GUIDE but an axes? How can I solve this problem in a easier manner? As for know the only way to redo the graphs is restarting the application...

Answers (1)

Joseph Cheng
Joseph Cheng on 4 May 2015
well there a few things you could do. Both require some trickery in matlab as far as i know subplots are not supported for GUIDE as part of the gui. meaning subplots are supported when making figures but not as hot swap of the drawn axes. As each subplot is an axes the handles gets over written. However with some trickery you can create your own function.
So in my example here i created a simple GUI in guide with two pushbuttons and an axes drawn for the region i want plots to be in.
the callback for pushbutton1 creates the subplots in the region selected by the drawn region in GUIDE.
function pushbutton1_Callback(hObject, eventdata, handles)
%generate some random plots
x1 = [0 1];
y1 = [0 1];
x2 = [0 2];
y2 = [0 5];
%get the GUI handles
hfig(1)=gcf;
%set units to pixels so we know the size of the plotting region.
set(handles.axes1,'units','pixels')
axpos = get(handles.axes1,'position');
set(handles.axes1,'visible','off'); %make the region invisible
%create temporary figure
hfig(2)=figure(200);
set(hfig(2),'position',axpos)
%set figure area to be the same size as the plotting area with same units
subplot(2,1,1),plot(x2,y2);subplot(2,1,2),plot(x1,y1);
ax2 = get(hfig(2),'children');
set(ax2,'units','pixels');
%copy plotted subplots to the gui
hsubplot = copyobj(ax2,hfig(1));
close(hfig(2)) % close the temporary figure
%get the position and move them into specified area
subpos = cell2mat(get(hsubplot,'position'));
subpos(:,1:2) = subpos(:,1:2)+repmat(axpos(1:2),2,1);
for ind = 1:length(hsubplot)
set(hsubplot(ind),'position',subpos(ind,:))
end
%save the subplot handles so they can be deleted later.
handles.hsubplots = hsubplot;
guidata(hObject, handles);
Then with a second pushbutton
function pushbutton2_Callback(hObject, eventdata, handles)
delete(handles.hsubplots)
set(handles.axes1,'visible','on')
we can delete and restore the original axes if you don't want to plot subplots.
Now spending more time one can clean this up. As if you know the region you don't need the axes1 to be create at all. However i'm sure you'll be able to adapt this to how you want to proceed.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!