Passing axes name as an argument then applied to to gca

1 view (last 30 days)
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

Accepted Answer

Adam
Adam on 16 Dec 2015
Edited: Adam on 16 Dec 2015
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.
  4 Comments
Jason
Jason on 17 Dec 2015
Edited: Jason on 17 Dec 2015
My only reluctance to keeping track of imrect i.e.
hFH = imrect();
and then using delete(hFH), is its only applicable afte rthe 2nd press of my pushbutton callback as there will already be an imrect on my image.
The first time I press the pushbutton there isn't one. Is there a way to see if hFH exists, and only if it does, then to delete it (before drawing the new one)?
I've just notice imrect has the following properties:
hFH =
imrect with properties:
Deletable: 1
Adam
Adam on 17 Dec 2015
Edited: Adam on 17 Dec 2015
When I do something like this I create the empty variable upfront. Then I will add rectangles to it as and when I create them. If no rectangles have been created delete still works on the empty variable.
e,g,
hFH = [];
delete( fH );
hFH = [hFH imrect()];
or
hFH( end + 1 ) = hRect();
So in this case delete just works on an empty variable so no need to test anything.
In general though I prefer to test if a variable is empty rather than whether it exists yet. In different situations I have done both though - i.e.
if exist( 'hFH', 'var' )
...
end
(Note the variable is in a string as its name, not just the variable itself)
or
if ~isempty( hFH )
...
end

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks 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!