event.PropertyEvent Class
Namespace: event
Data for property events
Description
The event.PropertyEvent
class defines the event data objects passed
to listeners of the predefined property events PreGet
,
PostGet
, PreSet
, and
PostSet
. Predefined property events enable listeners to respond
to changes made to property values. For more information, see Listen for Changes to Property Values.
The event.PropertyEvent
class is a sealed subclass of
event.EventData
(that is, you cannot subclass
event.PropertyEvent
). The class constructor is private.
MATLAB® creates an event.PropertyEvent
object to pass to
listeners of property events.
The event.PropertyEvent
class is a handle
class.
Class Attributes
Sealed | true |
ConstructOnLoad | true |
HandleCompatible | true |
RestrictsSubclassing | true |
For information on class attributes, see Class Attributes.
Properties
Examples
Listen for Property Event
Define the propEventClass
class with the
PropOne
GetObservable
and SetObservable
attributes
enabling observation of property events. The class constructor adds listeners for
these events.
classdef propEventClass < handle % Class to observe property events properties (GetObservable,SetObservable) PropOne string = "default" end methods function obj = propEventClass addlistener(obj,'PropOne','PreGet',@propEventHandler); addlistener(obj,'PropOne','PostSet',@propEventHandler); end end end
The propEventHandler
function serves as the callback for the
PreGet
and PostSet
events.
The event.PropertyEvent
object Source
property contains the matlab.metadata.Property
object for
PropOne
. Access the
matlab.metadata.Property
Name
property to get the name of the property on which the event
is triggered. Switch on the property name when the callback handles multiple
properties.
The event.PropertyEvent
object EventName
property contains the name of the event. To handle multiple property events from the
callback, switch on the event name.
function propEventHandler(~,eventData) switch eventData.Source.Name % Get property name case 'Prop1' switch eventData.EventName % Get the event name case 'PreGet' fprintf('%s\n','***PreGet triggered***') case 'PostSet' fprintf('%s\n','***PostSet triggered***') disp(eventData.AffectedObject.(eventData.Source.Name)); end end end
Referencing the PropOne
property value results in a response
from the propEventHandler
to the PreGet
event.
obj = propEventClass; obj.PropOne
***PreGet triggered*** ans = "default"
Assigning to the PropOne
property results in a response from
the propEventHandler
to the PostSet
event.
Because the callback gets the property value to display the new value after the
PostSet
event, the PreGet
event is
triggered. Also, because the assignment statement is not terminated by a semicolon,
MATLAB gets the property value to display the object in the command window,
which triggers the PreGet
event again.
obj.PropOne = "New string"
***PostSet triggered*** ***PreGet triggered*** New string obj = ***PreGet triggered*** propEventClass with properties: PropOne: "New string"
Version History
Introduced in R2008a