Listener doesnt display messages after changes through workspace

Hello, I tried to practice with event and listener objects.
As I practiced, I made two classes, one with an event and one with a listener function upon the event.
I noticed that the listener doesnt display messages if the event is trigerred through the workspace.
The event code:
classdef EventPractice < handle
properties (SetObservable = true)
NumEvent = 1
end
events
NumChangedEvent
end
methods
function set.NumEvent(obj,value)
obj.NumEvent = value;
notify(obj,'NumChangedEvent');
end
end
end
The listener code:
classdef ListenerPractice < handle
properties
NumListener = 1
end
methods
function obj = ListenerPractice(EventSrc)
addlistener(EventSrc,'NumChangedEvent',@obj.ListenerReaction);
end
end
methods
function ListenerReaction(obj,EventSrc,~)
obj.NumListener = EventSrc.NumEvent
disp(['The event Num has changed. The listener Num changes to the same num: 'num2str(EventSrc.NumEvent)])
end
end
end
Creation Code:
EventExample = EventPractice;
ListenerExample = ListenerPractice(EventExample);
Change at the Workspace:
Change reaction at the ListenerExample variable:
Empty Command Window:
After this whole process, If the NumListener variable changed at the listener function 'ListenerReaction', why didnt the disp() function also go into action?
Thank you for reading,
Yuval Blutman.

4 Comments

This might be because only thorugh the setNumEvent function, you can trigger the event.
Also there are syntax errors in your code
  • In event how did you call the function set.NumEvent ? I dont think there should be a dot
  • In listener, it is throwing me an error of missing comma before num2str in display function
And in my case any value change if changed in workspace. Can you explain how you did that ?
But it is working fine from command line if I call the setNumEvent function.
Thank you for your answer Nikhil.
the set command in this syntax is called whenever a property is set. so for example here when I change NumEvent the set function is called. I can see that it is indeed called so the problem is not with the syntax of the set function.
On my end the value of the listener property NumListener is modified when I change NumEvent through the workspace. however the message that should be displayed ('The event Num has changed. . .') is not displayed. it is displayed only if I change NumEvent through code or through the command line.
@Yuval Blutman: Please share the actual code you are running; as @Nikhil pointed out, there are syntax errors in the code posted here:
1:
function set.NumEvent(obj,value)
% ^ period is not allowed in a function name
2:
disp(['The event Num has changed. The listener Num changes to the same num: 'num2str(EventSrc.NumEvent)])
% ^^ missing comma or space before num2str
Hello, about the set.NumEvent function, it does work in my end. And for the missing comma or space, It is indeed missing and probably got cut as i copied the code. I will add here the code files themselves and this is the actual code line:
disp(['The event Num has changed. The listener Num changes to the same num: ' num2str(EventSrc.NumEvent)])
I also emphasize again, the issue I brought up is about changing variables through the WorkSpace, and the listener function partialy reacting (not activating display function). It is described at the main thread.
Thank you for the reply,
Yuval Blutman.

Sign in to comment.

Answers (1)

Hi Yuval,
As I have mentioned earlier , your NumChangedEvent is triggered only when the setNumEvent function is called ( which is the case when you call the function ).
When you change the value throught workspace, no event is triggered and hence no display.
Even if you try something like objName.NumEvent = 10 from command line, the display function won't work as no event is triggered.
It is triggered only if you do objName.setNumEvent(10) ( literally call the function).
The only thing I am not able to figure out is, when I change value from workspace , the value is changed only in event and not listener.
Hope this helps.

Asked:

on 10 Dec 2022

Answered:

on 19 Dec 2022

Community Treasure Hunt

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

Start Hunting!