listener
Creates an event listener without attaching it to the life of the source object.
Category: Programming > Classes
Syntax
lh = listener(obj, 'EventName', callback)
Creates a listener that calls callback when obj triggers the event.
Description
listener and addlistener build the same kind of object. The difference is lifetime: a listener made with listener stays active only while you hold lh, so it disappears when the variable is cleared or goes out of scope. That makes it the right tool when the listener should be tied to a view or a temporary computation rather than to the source.
Setting lh.Enabled = false pauses the callback without removing the listener.
Input arguments
obj- Handle object that defines the event.
'EventName'- Name of the event.
callback- Function handle called as
callback(src, evtdata).
Output arguments
lh- An
event.listenerobject with propertiesSource,EventName,Callback,EnabledandRecursive.
Example
Creating and pausing a listener
m = containers.Map();
lh = listener(m, 'ObjectBeingDestroyed', @(s, e) disp('gone'));
lh.EventName
lh.Enabled
lh.Enabled = false;
lh.Enabled
Output
ans = 'ObjectBeingDestroyed'
ans = logical
1
ans = logical
0