MATLAB: Passing axes name as an argument then applied to to gca

axesfunctiongcahandles

I have recently asked how to pass an axes name to an function and Walter has kindly suggested the following:
function rescaleImage(handles,str)
%
Get image on axes1
axes(handles.axes1)
image1 = getimage(handles.axes1);
%Create handle for new placement of image
%Use str argument to pass the axes handle
imshow(image1,[], 'Parent', handles.(str))
I now want to do something similar but using the same idea doesn't work.
axes(handles.axes1);
delete(findobj(gca,'type','line'))
This works but I want "axes1" to be passed as an argument called str, but when I now try
delete(findobj('Parent', handles.(str),'type','line'))
it fails.
Any advice?
Thanks Jason

Best Answer

I would strongly advise keeping hold of the handles to the graphics objects you plot so you can delete them later if that is what you want to do.
findobj should be a last resort really as it is far easier to just keep a handle to a line object (or an array of them) and call delete on that than having to fish them out from an axes.
If you have the axes handle, hAxes, you can do something like
hChildren = get( hAxes, 'Children' )
hLines = hChildren( arrayfun( @(x) strcmp( class(x), 'matlab.graphics.chart.primitive.Line' ), hChildren ) )
delete( hLines );
with e.g.
hAxes = handles.( str )
if you wish.
to get all the line objects on the axes, but that is similar to findobj in terms of having to refind the line objects rather than just having stored them in the first place.