MATLAB: Clear axes – Except image

claclear axesdeletereset

I got a GUI with several different axes, for different images. I plot some scatters and lines on these images. But it is a mess to clear, so I would like to know if there is a simple an easy way to clear an axes,something like cla(-except image), rather than testing for existing scatters and then use delete or reset.

Best Answer

You can use findobj() like in this function that I often use:
%=====================================================================
% Erases all lines and text from the current axes.
% The current axes should be set first using the axes() command
% before this function is called, as it works from the current axes, gca.
function ClearLinesFromAxes(handles)
try
% Clear line objects.
axesHandlesToChildObjects = findobj(gca, 'Type', 'line');
if ~isempty(axesHandlesToChildObjects)
delete(axesHandlesToChildObjects);
end
% Clear text objects.
axesHandlesToChildObjects = findobj(gca, 'Type', 'text');
if ~isempty(axesHandlesToChildObjects)
delete(axesHandlesToChildObjects);
end
catch ME
message = sprintf('Error in ClearLinesFromAxes():\n%s', ME.message);
uiwait(warndlg(message));
end
return; % from ClearLinesFromAxes
Of course you can also do this:
axes(handlesToAxes);
cla reset;
imshow(yourImage);
This might also be pretty fast, though there's a possibility it may flash to white for a fraction of a second before your image redisplays.