make axes invisible or delete plot completely

2020-04-08 12:36发布

问题:

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.

回答1:

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


回答2:

I use this:

    set(allchild(handles.axes1),'visible','off'); 
    set(handles.axes1,'visible','off'); 

for hiding my axes. I found the solution here: Visible axes off



回答3:

You have use the handles to the subplots and plots:

h(1)=subplot(221);
    p(1)=plot(rand(10,1));
h(2)=subplot(222);
    p(2)=plot(rand(10,1));
h(3)=subplot(223);
    p(3)=plot(rand(10,1));
h(4)=subplot(224);
    p(4)=plot(rand(10,1));


set([h(2) p(2)],'visible','off')

hides the 2nd plot. @Jonas answer seems more complete however. It's certainly easier, because you don't have to collect the children yourself manually as I did here.



回答4:

I now solved it with

function z_removePlots(handles)

if (isfield(handles,'image') && isfield(handles.image,'hplot'))
    if ishandle(handles.image.hplot)
        delete(handles.image.hplot);
        delete(findall(gcf,'tag','Colorbar'));
        handles.image.hplot = 0;
        set(handles.axesImage, 'Visible', 'off');
    end
end
if (isfield(handles,'contour') && isfield(handles.contour,'hplot'))
    if ishandle(handles.contour.hplot)
        delete(handles.contour.hplot);
        handles.contour.hplot = 0;
        ClearLinesFromAxes(handles.axesContour)
        set(handles.axesContour, 'Visible', 'off');
    end
end
guidata(handles.output,handles);

with

function ClearLinesFromAxes(axisObj)
if ishandle(axisObj)
    handles2delete = get(axisObj,'Children');
    delete(handles2delete);
end


标签: matlab axes