| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
lh = event.listener(Hobj,'EventName',@CallbackFunction)
The event.listener class defines listener objects. Listener objects respond to the specified event and identify the callback function to invoke when the event is triggered.
lh = event.listener(Hobj,'EventName',@CallbackFunction) creates an event.listener object, lh, for the event named in EventName, on the on the specified object, Hobj.
If Hobj is an array of object handles, the listener responds to the named event on any of the objects referenced in the array
The listener callback function must accept at least two input arguments. For example,
function CallbackFunction(source,eventData) ... end
where source is the object that is the source of the event and eventData is an event.EventData object.
The event.listener class is a handle class.
Generally, you create a listener object using addlistener. However, you can call the event.listener constructor directly to create a listener. However, when you do not use addlistener, the listener's lifecycle is not tied to the object(s) being listened to—once the listener object goes out of scope, the listener no longer exists. See Ways to Create Listeners for more information on creating listener objects.
If you call delete(lh) on the listener object, the listener ceases to exist, which means the event no longer causes the listener callback function to execute.
You can enable or disable a listener by setting the value of the listener's Enabled property (see Properties table below).
See Events — Sending and Responding to Messages for more information and examples of how to use events and listeners.
| Property | Purpose |
|---|---|
| Source | Cell array of source objects |
| EventName | Name of the event |
| Callback | Function to execute when the event is triggered and the Enabled property is set to true |
| Enabled | callback executes when the event occurs if and only if Enabled is set to true (the default). |
| Recursive | When this property is set to true (the default), a listener can cause the same event that triggered the callback. This can lead to infinite recursion and the MATLABrecursion limit eventually triggers an error to end the recursion. When set to false, this listener does not execute recursively. Therefore, if the callback triggers its own event, the listener does not execute again. |
addlistener, delete, event.proplistener
![]() | event.PropertyEvent | event.proplistener | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |