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

2 Ansichten (letzte 30 Tage)
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 Kommentar
Jacob Lynch August
Jacob Lynch August am 13 Mär. 2020
Bearbeitet: Jacob Lynch August am 13 Mär. 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

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Construct and Work with Object Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by