event.listener not working when called within class instance
조회 수: 3 (최근 30일)
이전 댓글 표시
Dear Forum,
I encounter some problems when using event.listener class. For testing I've implemented the following code:
Event-triggering class:
classdef Subject < handle
events
event
end
properties
end
end
Responding class:
classdef Object < handle
properties
listen;
end
methods
function setListener(obj, subject, event)
%obj.listen = event.listener(subject, event, @obj.respond);
obj.listen = addlistener(subject, event, @obj.respond);
end
end
methods
function respond(varargin)
disp('Event has occured');
end
end
end
And a tiny test script:
% create instances
subject = Subject();
object = Object();
% set listener
object.setListener(subject, 'event')
% notify object
notify(subject, 'event');
When switching the statements
%obj.listen = event.listener(subject, event, @obj.respond);
obj.listen = addlistener(subject, event, @obj.respond);
in class Object, I will get the following error:
Struct contents reference from a non-struct array object.
Error in Object/setListener (line 12)
obj.listen = event.listener(subject, event, @obj.respond);
Error in testListener (line 6)
object.setListener(subject, 'event')
To my understanding this should work.
Moreover, I can establish the connection, when creating the listener from my script:
l = event.listener(subject, 'event', @object.respond)
Is it not possible to call event.listener from within an object?
I am using Matlab 2016a.
Can someone please help?
Thanks,
Daniel
댓글 수: 0
답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Software Development Tools에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!