event.PropertyEvent - Data for property events - MATLAB (original) (raw)
Namespace: event
Description
The event.PropertyEvent
class defines the event data objects passed to listeners of the predefined property events PreGet
,PostGet
, PreSet
, andPostSet
. 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 ofevent.EventData
(that is, you cannot subclassevent.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
Object whose property is affected, specified as the object handle.
Attributes:
GetAccess | public |
---|---|
SetAccess | private |
GetObservable | true |
SetObservable | true |
Data Types: handle object
Property that triggers the event, specified as thematlab.metadata.Property
object for the property.
Attributes:
GetAccess | public |
---|---|
SetAccess | private |
GetObservable | true |
SetObservable | true |
Name of the property event, specified as one of the four event names.
Attributes:
GetAccess | public |
---|---|
SetAccess | private |
GetObservable | true |
SetObservable | true |
Data Types: char
Examples
Listen for Property Event
Define the propEventClass
class with thePropOne
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 thePreGet
and PostSet
events.
The event.PropertyEvent
object Source
property contains the matlab.metadata.Property
object forPropOne
. Access thematlab.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 thePostSet
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