1

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?

Community
  • 1
  • 1
Matthias Pospiech
  • 3,130
  • 18
  • 55
  • 76

1 Answers1

1

This snippet works fine here...

In general, it is advisable to check for ishandle() on both objects, so that you don't have to set handles.image.hplot = 0. Since delete has made the handle invalid, it will never pass the ishandle check until you re-assign a new, valid handle to it.

If the code doesn't pass the ishandle() tests, it means it was deleted already, so no need to delete it again.

Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96