Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

Events and Listeners

Define and Trigger Events

To define an event, declare a name for the event in an events block. Trigger the event using the handle class notify method. Only classes derived from the handle class can define events.

For example, MyClass class:

  • Subclasses handle

  • Defines an event named StateChange

  • Triggers the event using the inherited notify method in its upDateUI method.

classdef MyClass < handle 
   events 
      StateChange
   end
   ...
   methods
      function upDateUI(obj)
         ...
         notify(obj,'StateChange');
      end
   end
end

Listen for Events

Any number of objects can listen to the StateChange event. When notify executes, MATLAB® calls all registered listener callbacks. MATLAB passes the handle of the object generating the event and event data to the callback functions. To create a listener, use the addlistener method of the handle class.

addlistener(event_obj,'StateChange',@myCallback)

Related Examples

Was this topic helpful?