Search code examples
matlabaxes

make axes invisible or delete plot completely


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.


Solution

  • 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