Ho to delete the image from the axes?

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

 Accepted Answer

Image Analyst
Image Analyst on 23 Dec 2013
Edited: Image Analyst on 23 Dec 2013
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

3 Comments

Now if i have two axes in gui and upon pressing clear button,I want both the images to be removed.
if true
% code
end
function Clear_Callback(hObject, eventdata, handles)
axis on
ClearImagesFromAxes(handles.axes1)
set(handles.axes1,'xtick',[],'ytick',[]);
ClearImagesFromAxes(handles.axes2)
set(handles.axes2,'xtick',[],'ytick',[]);
function ClearImagesFromAxes(h)
axesHandlesToChildObjects = findobj(h, 'Type', 'image');
if ~isempty(axesHandlesToChildObjects)
delete(axesHandlesToChildObjects);
end
return; % from ClearImagesFromAxes
The problem is upon writing this code the first axes gets completely deleted while the image from the axes2 is deleted only.
Try "axis on" for the second axis too. Your axis on only applied to the current axes, which presumably was your axes1, so it never got turned on for axes2.
Thanks sir. It worked by adding axes(handles.axes2);axis on

Sign in to comment.

More Answers (1)

Try
cla(handles.axes1)

2 Comments

I didn't think about that one ('cla') and it worked great for me !
But it clears more than just the image (which is what the original poster wanted to restrict it to). If you want to clear everything, then that's fine - it will work.

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!