1

I'm trying to create a class similar to the Line object's class in MATLAB 2017b but having problem with the destructor. I want to achieve a similar behavior with other graphic objects that when deleted the handle is no longer referencing to that object, like in the following example:

 >> l1 = line([0 1], [0 1]);

If the figure containing line l1 is now closed, the variable shows that l1 is referencing to a deleted object:

>> l1

l1 = 

  handle to deleted Line

I created the following class:

classdef circle < handle ...
        & matlab.mixin.CustomDisplay & matlab.mixin.SetGet

    properties
        Center = [];
        Color = [0 0.4470 0.7410];
        Radius = [];
    end

    properties (SetAccess = 'protected')
        LineHandle = [];
    end

    methods
        function obj = circle(radius, center, varargin)
            if nargin > 0
                % assign property values
                obj.Radius = radius;
                obj.Center = center;

                % generate plotting variables
                phi = linspace(0, 2*pi, 90);
                x = radius*cos(phi) + center(1);
                y = radius*sin(phi) + center(2);

                % draw circle
                obj.LineHandle = line(x, y);

                % create listeners in line object
                obj.createListener;

                % set variable properties
                for k = 1:2:length(varargin)
                    % set superclass properties
                    if (isprop(obj.LineHandle, varargin{k}))
                        set(obj.LineHandle, varargin{k},varargin{k+1});
                        if (isprop(obj, varargin{k}))
                            set(obj, varargin{k}, varargin{k+1});
                        end
                    end        
                end
            end
        end

        % listener to invoke delete if line is closed
        function createListener(obj)         
            set(obj.LineHandle,'DeleteFcn',...
                @obj.delete);
        end

         function delete(obj,varargin)
            disp('deleted')
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            % command to delete class ???
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%
         end

    end

end

If the figure containing a circle object is closed, the line is deleted but the circle object still existst. Is there a way to also delete the reference to the circle object?

RudiRocker
  • 33
  • 5
  • 1
    If you check `class(l1)` of a deleted line it is still a Line object instance (`matlab.graphics.primitive.Line`). I suspect that there's an internal flag (e.g. hidden property) of the object that marks it as deleted. You could maintain such a flag in your class (and have it set in the `.delete` method). I see you have already derived your class from `matlab.mixin.CustomDisplay`, so in that case you just need to define `getHeader`, `getPropertyGroups` and `getFooter` methods for your circle class. Have the `getHeader` return "handle to deleted Circle" and nothing for PropertyGroups or Footer? – Justin Aug 14 '18 at 15:27
  • 1
    It may also help to define `isvalid()` to return 0 after being deleted? – Justin Aug 14 '18 at 15:30
  • I defined methods for `getHeade`, `getPropertyGroups`and `getFooter`, that work quiet well. But how can I get `isvalid` to return 0 after being deleted? I tried to implement a method but get the following error `Method 'isvalid' in class 'circle' conflicts with the Sealed method from the superclass definition in 'handle'.` – RudiRocker Aug 16 '18 at 08:07

1 Answers1

0

I found out the answer myself. The function just needs to have a different name than delete:

function delete_obj(obj, varargin)
        % delete handle
        delete(obj)
end
RudiRocker
  • 33
  • 5