Delete MATLAB subclass of graphics primitive

111 Views Asked by At

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?

1

There are 1 best solutions below

0
On

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