MATLAB: Using the handle of a rectangle to first delete and then re-add to an image

handlesimagerectangle

Hello. I superimpose a rectangle on a loaded image displayed on an axes component. I need the rectangle central, and size determined by a variable 'delta' as below.
img=imread('c:\Image03.tif');
axes(handles.axes1)
imageHandle=imshow(img,[]);
[rows,cols]=size(img)
rm=round(rows/2)
cm=round(cols/2)
delta=200
x=cm-delta
y=rm-delta
hold on;
h1=rectangle('Position',[x,y,delta,delta], 'FaceColor','none', 'EdgeColor','r')
hold off;
I then save this rectangle to the handles structure, so I can use it later.
handles.ROIrect=h1
guidata(hObject, handles);
I then use another button's callback to delete the rectangle fromt he image through:
axes(handles.axes1)
delete( findobj(gca, 'type', 'rectangle') );
I then want to have another pushbutton with its callback to redraw the rectangle on the image. I thought the following would work but it doesn't.
axes(handles.axes1)
h1=handles.ROIrect
Have i made an obvious mistake>?
Thanks

Best Answer

This cannot be done. delete() is defined as meaning to track down all the actual object information and remove the object from the graphics system, destroying everything about it. delete() is like shredding a piece of paper rather than putting it in the recycle bin.
If you want a graphics object to stop being visible, then set its Visible property to off, or move it to a different parent that is not visible.