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

3 次查看(过去 30 天)
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 个评论
Jacob Lynch August
Jacob Lynch August 2020-3-13
编辑:Jacob Lynch August 2020-3-13
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

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Construct and Work with Object Arrays 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by