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

조회 수: 1 (최근 30일)
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.

채택된 답변

per isakson
per isakson 2014년 3월 31일
편집: per isakson 2014년 4월 1일
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
Matt J
Matt J 2014년 4월 1일
편집: Matt J 2014년 4월 1일
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 2014년 4월 1일
Yes, of course. When you mention it, I recall reading about ObjectBeingDestroyed. The language is growing large.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Class Introspection and Metadata에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by