Passing axes name as an argument then applied to to gca

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

 Réponse acceptée

Adam
Adam le 16 Déc 2015
Modifié(e) : Adam le 16 Déc 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 commentaires

Jason
Jason le 17 Déc 2015
Modifié(e) : Jason le 17 Déc 2015
Thanks Adam. The rectangle I want to delete are a result of using imrect, and hence its the imrect's below I need to delete. Using 'line' does not delete them.
As youc an see, everytime I call imrect in a pushbutton callback it leaves the outline from the rectangle. (The yellow rectangle is for reference and i need it to stay)
Group (imrect)
Group (imrect)
Rectangle
Image
Adam
Adam le 17 Déc 2015
Modifié(e) : Adam le 17 Déc 2015
imrect returns its graphics handle as an output so just keep hold of these when you create one and add them to an array of created imrect handles. If you then just want to delete all of them it is simple. If you want to delete specific ones then you will need to keep some other form of information with each imrect graphics handle to identify which is which.
Keep track of the yellow rectangle separately if you know which it is in advance. Otherwise keep some indicator array to identify it by and delete the rest.
e.g.
delete( hRects( [1:4 6:8] ) );
if hRect(5) is to be kept.
Jason
Jason le 17 Déc 2015
Modifié(e) : Jason le 17 Déc 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 le 17 Déc 2015
Modifié(e) : Adam le 17 Déc 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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Interactive Control and Callbacks dans Centre d'aide et File Exchange

Question posée :

le 16 Déc 2015

Modifié(e) :

le 17 Déc 2015

Community Treasure Hunt

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

Start Hunting!

Translated by