Main Content

event.PropertyEvent class

Package:event

Data for property events

Description

Theevent.PropertyEventclass defines the event data objects passed to listeners of these predefined property events:

  • PreGet

  • PostGet

  • PreSet

  • PostSet

Predefined property events enable listeners to respond to changes made to property values. For more information, seeListen for Changes to Property Values.

Theevent.PropertyEventclass is a sealed subclass ofevent.EventData(that is, you cannot subclassevent.PropertyEvent). The class constructor is private. MATLAB®creates anevent.PropertyEventobject to pass to listeners of property events.

Theevent.PropertyEventclass is ahandleclass.

Class Attributes

Sealed
true
ConstructOnLoad
true
HandleCompatible
true
RestrictsSubclassing
true

For information on class attributes, seeClass Attributes.

Properties

expand all

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 themeta.propertyobject for the property.

Attributes:

GetAccess
public
SetAccess
private
GetObservable
true
SetObservable
true

Data Types:meta.property

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 thepropEventClass类的PropOneGetObservableandSetObservableattributes enabling observation of property events. The class constructor adds listeners for these events.

classdefpropEventClass < handle%一堂课s to observe property eventsproperties(GetObservable,SetObservable) PropOnestring="default"endmethodsfunctionobj = propEventClass addlistener(obj,'PropOne','PreGet',@propEventHandler); addlistener(obj,'PropOne','PostSet',@propEventHandler);endendend

ThepropEventHandlerfunction serves as the callback for thePreGetandPostSet事件。

Theevent.PropertyEventobject所以urceproperty contains themeta.propertyobject forPropOne. Access themeta.propertyNameproperty to get the name of the property on which the event is triggered. Switch on the property name when the callback handles multiple properties.

Theevent.PropertyEventobjectEventNameproperty contains the name of the event. To handle multiple property events from the callback, switch on the event name.

functionpropEventHandler(~,eventData)switcheventData.Source.Name% Get property namecase'Prop1'switcheventData.EventName% Get the event namecase'PreGet'fprintf('%s\n','***PreGet triggered***')case'PostSet'fprintf('%s\n','***PostSet triggered***') disp(eventData.AffectedObject.(eventData.Source.Name));endendend

Referencing thePropOneproperty value results in a response from thepropEventHandlerto thePreGetevent.

obj = propEventClass; obj.PropOne
***PreGet triggered*** ans = "default"

Assigning to thePropOneproperty results in a response from thepropEventHandlerto thePostSetevent.

Because the callback gets the property value to display the new value after thePostSetevent, thePreGetevent 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 thePreGet事件再次。

obj.PropOne ="New string"
***PostSet triggered*** ***PreGet triggered*** New string obj = ***PreGet triggered*** propEventClass with properties: PropOne: "New string"
Introduced in R2008a