Matlab Events and Listeners

April 18th, 2010 Posted in Documents

Specifying Events

To define an event, you declare a name for the event in the events block. Then one of the the event using the , which is inherited from the handle class. Only classes derived from the handle class can define events.

For example, the following class:

  • Defines an event named StateChange
  • Triggers the event using the inherited notify method.
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    classdef class_name < handle % Subclass handle
       events % Define an event called StateChange
          StateChange
       end
       ...
       methods
          function upDateGUI(obj)
             ...
             % Broadcast notice that StateChange event has occurred
             notify(obj,'StateChange');
          end
       end
    end

    Listening for Events

Any number of objects can be listening for the StateChange event to occur. When notify executes, MATLAB calls all registered listener callbacks and passes the handle of the object generating the event and an event structure to these functions. To register a listener callback, use the addlistener method of the handle class.

1
addlistener(event_obj,'StateChange',@myCallback)

Leave a Reply

You must be logged in to post a comment.