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

5 ビュー (過去 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 Center および File ExchangeConstruct 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