Trigger deletion of handle object when one of its IMROI properties is deleted

1 view (last 30 days)
I have a handle class one of whose properties is an imroi object hande (e.g., as generated by imellipse),
classdef myclass < handle
properties
prop=1;
H=imellipse;
end
methods
function delete(obj)
...
end
end
end
I would like to make it so that when the imellipse object (pointed to by obj.H) is deleted from the figure it resides in, the myclass delete() method is triggered on obj. I imagine there is some way to do it with listener objects, but I am new to the subject of listeners and could use some guidance. For one thing, I cannot see how to customize the imroi class' delete() method to issue the notify() command.

Accepted Answer

per isakson
per isakson on 31 Mar 2014
Edited: per isakson on 1 Apr 2014
Here is an example. Since I don't want to modify the Matlab functions, I have subclassed imellipse.
Step 1. Execute this script
figure, imshow( 'cameraman.tif' );
mye = my_ellipse( gca, [10 10 100 100] );
ear = my_ear( mye );
Step 2. Delete circle interactively. The following line will dispay in the command window
I hear you load and clear!
>>
where
classdef my_ellipse < imellipse
properties
end
events
ImDeleted
end
methods
function this = my_ellipse( varargin )
this = this@imellipse( varargin{:} );
end
function delete( this )
notify( this, 'ImDeleted' )
end
end
end
and
classdef my_ear < handle
properties
ear
end
methods
function this = my_ear( sender )
this.ear = addlistener( sender, 'ImDeleted', @this.gotIt );
end
function gotIt( this, varargin )
disp( 'I hear you load and clear!' )
end
end
end
The delete methods of the ancestors are executed automagically. (I don't find the line describing this magic in the documentation. And there is a "strong recommendation" not to cause run time errors in the delete methods.)
.
Continuation:
Run
figure, imshow( 'cameraman.tif' );
axh = gca;
pos = [10 10 100 100];
myc = my_class( axh, pos );
and delete the circle object, which will output a line to the command window
my_class.delete is executed
>>
where
classdef my_class < handle
properties
H
ear
end
methods
function this = my_class( axh, pos )
this.H = my_ellipse( axh, pos );
this.ear = addlistener( this.H, 'ImDeleted', @this.delete );
end
function delete( this, varargin )
disp( 'my_class.delete is executed' )
end
end
end
.
The documentation says:
[The delete method] Must have [exactly] one scalar input argument
that is an object of the class.
Thus, function delete( this, varargin ) is wrong. However, I wonder whether varargin is an exception.
  2 Comments
Matt J
Matt J on 1 Apr 2014
Edited: Matt J on 1 Apr 2014
Thanks, per. Actually, though, I discovered that you don't need to subclass imellipse. As a handle subclass itself, imellipse inherits an event called 'ObjectBeingDestroyed', which can be used as below.
classdef myclass < handle
properties
prop=1;
H
end
methods
function this=myclass
H=imellipse;
this.H=H;
addlistener(H,'ObjectBeingDestroyed',@this.delete);
end
function delete(this,varargin)
if isvalid(this.H)
%delete() was triggered by "delete(this)", not the listener
delete(this.H);
end
...
end
end
end
per isakson
per isakson on 1 Apr 2014
Yes, of course. When you mention it, I recall reading about ObjectBeingDestroyed. The language is growing large.

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!