make axes invisible or delete plot completely

Matthias Pospiech picture Matthias Pospiech · Aug 8, 2012 · Viewed 15.6k times · Source

I have a matlab gui that shall contain 4 plots. The first plot shall be updated if a different file is selected in a list. the other 3 shall only be visible (and be calculated) on request.

However I fail to make plots 2-4 invisible after they have been plotted once.

I tried

set(handles.axesImage, 'Visible', 'off');

but that only deletes the axis, not the whole plot.

EDIT: Instead of making things unvisible, is it also possible to actually delele the content? Typically I would call close(hfig);, but here i have no figure.

I tried

handles2hide = [axisObj;cell2mat(get(axisObj,'Children'))]; 
delete(handles2hide);

But that fails for the unplotted axes (after startup)

EDIT: I changed the code to:

axisObj = handles.axesContour;
if ishandle(axisObj)
    handles2delete = get(axisObj,'Children');
    delete(handles2delete);
    set(axisObj,'visible','off') 
end
if (isfield(handles,'contour') && isfield(handles.contour,'hColorbar'))
    delete(handles.contour.hColorbar);
    delete(handles.contour.hColorbarLabel);
end

However the colorbar remains undeleted and handles.contour.hColorbar fails with Invalid handle object.

Answer

Jonas picture Jonas · Aug 8, 2012

You want to hide not only the axes, but all of their children:

handles2hide = [handles.axesImage;cell2mat(get(handles.axesImage,'Children'))];
set(handles2hide,'visible','off')

The cell2mat is needed only if there are more than one handle stored in handles.axesImage

Note that you'll need the full list of handles to make everything visible again.

EDIT

If you want to delete all axes (includes colorbars) and their children on a figure, you can do the following (if you have to exclude certain axes, you can use setdiff on the lists of handles):

ah = findall(yourFigureHandle,'type','axes')
if ~isempty(ah)
   delete(ah)
end