MATLAB: Ho to delete the image from the axes

axesimageremove

I want to remove the image from the axes(say axes1) upon clicking a button keeping the axes intact.
I have tried using
children=get(handles.axes1, 'children');
delete(children);
and other things but it completely removes the axes.
Can anyone help??
Thanx in advance

Best Answer

You need to find the image in the axes with findobj() and then delete it. Like this:
axesHandlesToChildObjects = findobj(h, 'Type', 'image');
if ~isempty(axesHandlesToChildObjects)
delete(axesHandlesToChildObjects);
end
You can't just call cla() or it will delete everything such as text, arrows, overlays, or other annotation you added. Try this full demo:
function test3
% Display an image.
imshow('moon.tif');
axis on;
% Add some text.
text(100,250, 'Moon', 'FontSize', 30, 'Color', 'r');
promptMessage = sprintf('Do you want to clear the image only?');
titleBarCaption = 'Continue?';
button = questdlg(promptMessage, titleBarCaption, 'OK', 'Cancel', 'OK');
if strcmpi(button, 'OK')
% Get rid of image from current axes, but leave text.
ClearImagesFromAxes(gca)
end
%=====================================================================
% Erases all images from the axes.
function ClearImagesFromAxes(h)
axesHandlesToChildObjects = findobj(h, 'Type', 'image');
if ~isempty(axesHandlesToChildObjects)
delete(axesHandlesToChildObjects);
end
return; % from ClearImagesFromAxes