Trigger deletion of handle object when one of its IMROI properties is deleted
    2 vues (au cours des 30 derniers jours)
  
       Afficher commentaires plus anciens
    
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.
0 commentaires
Réponse acceptée
  per isakson
      
      
 le 31 Mar 2014
        
      Modifié(e) : per isakson
      
      
 le 1 Avr 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 commentaires
  per isakson
      
      
 le 1 Avr 2014
				Yes, of course. When you mention it, I recall reading about ObjectBeingDestroyed. The language is growing large.
Plus de réponses (0)
Voir également
Catégories
				En savoir plus sur Handle Classes dans Help Center et File Exchange
			
	Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

