Instantiating an invalid handle object when defining an object as default property definition

3 views (last 30 days)
A classdef defines some properties which are defined, but have no valid construction. E.g. an event.proplistener. In the example below, this class cannot be instantiated, generating the following error.
Error defining property 'Listener' of class 'egPropWithListener':
No constructor 'event.proplistener' with matching signature found.
This property could contain a deleted handle to an event.proplistener object. Is there a way to create a "pre-deleted" handle of an object?
classdef egPropWithListener < handle
properties(SetObservable,AbortSet)
Prop1 (1,1) double = 0;
end
properties(Access=protected)
% This listener monitors Prop1, and will
Listener (1,1) event.proplistener
end
methods
function obj = egPropWithListener()
obj.Listener = addlistener(obj,'Prop1','PostSet',@egPropWithListener.postSetPropAction);
end
end
methods(Static)
function postSetPropAction(prop_dat,event_dat)
obj = event_dat.AffectedObject;
% some actions ...
% if condition met ...
obj.Listener.Enable = false;
% ... stop listening.
end
end
end
  1 Comment
Jacob Lynch August
Jacob Lynch August on 13 Mar 2020
Edited: Jacob Lynch August on 13 Mar 2020
My colleagues that don't use MATLAB struggle with it's syntax. My current workarounds for their benefit, which I don't favor, involve relaxing the type or size defintion.
classdef egPropWithListener < handle
% ...
properties
% relax the type definition
AltDef1 (1,1) % event.proplistener
% relax the size defintion
AltDef2 (1,:) event.proplistener
% initiates with an empty vector of listeners, like event.proplistener.empty(1,0)
end
end

Sign in to comment.

Answers (0)

Categories

Find more on Construct and Work with Object Arrays 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!