MATLAB Object-Oriented Programming

(Joyce) #1

Create Listener for Overflow Event


To listen for the Overflow event, attach a listener to an instance of the
SimpleEventClass class. Use the addlistener method to create the listener. Also, you
must define a callback function for the listener to execute when the event is triggered.

The function setupSEC instantiates the SimpleEventClass class and adds a listener to
the object. In this example, the listener callback function displays information that is
contained in the eventData argument (which is a SpecialEventDataClass object).

function sec = setupSEC
sec = SimpleEventClass;
addlistener(sec,'Overflow',@overflowHandler)
function overflowHandler(eventSrc,eventData)
disp('The value of Prop1 is overflowing!')
disp(['Its value was: ' num2str(eventData.OrgValue)])
disp(['Its current value is: ' num2str(eventSrc.Prop1)])
end
end

Create the SimpleEventClass object and add the listener:

sec = setupSEC;
sec.Prop1 = 5;
sec.Prop1 = 15; % listener triggers callback

The value of Prop1 is overflowing!
Its value was: 5
Its current value is: 15

See Also


Related Examples



  • “Observe Changes to Property Values” on page 11-9


11 Events — Sending and Responding to Messages

Free download pdf