event.listener not working when called within class instance
4 次查看(过去 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!