I want to delete a plot and its colorbar ( actually I want to delete everything in a plot, but that seams to be almost impossible, see make axes invisible or delete plot completely)
I do this:
In the plot
hplot = pcolor(xAxis, yAxis, Data2D);
hcb = colorbar;
handles.image.hColorbar = hcb;
handles.image.hplot = hplot;
guidata(handles.output,handles);
later in the gui:
if (isfield(handles,'image') && isfield(handles.image,'hplot'))
if (handles.image.hplot~=0)
delete(handles.image.hplot);
delete(handles.image.hColorbar);
handles.image.hplot = 0;
end
end
It works for delete(handles.image.hplot) but fails for handles.image.hColorbar with invalid handle – why?
This snippet works fine here…
In general, it is advisable to check for
ishandle()on both objects, so that you don’t have to sethandles.image.hplot = 0. Sincedeletehas made the handle invalid, it will never pass theishandlecheck until you re-assign a new, valid handle to it.If the code doesn’t pass the
ishandle()tests, it means it wasdeletedalready, so no need todeleteit again.