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

조회 수: 2 (최근 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