MATLAB Object-Oriented Programming

(Joyce) #1

Set and Get Methods for Dynamic Properties


In this section...
“Create Access Methods for Dynamic Properties” on page 8-73
“Shared Set and Get Methods” on page 8-74

You can define property set access or get access methods for dynamic properties without
creating additional class methods. For general information on the use of access methods,
see “Property Access Methods” on page 8-50.

Create Access Methods for Dynamic Properties


Use these steps to create a property access method:


  • Define a function that implements the operations you want to perform before the
    property set or get occurs. These methods must have the following signatures:
    mySet(obj,val) or val = myGet(obj)

  • Obtain the dynamic property's corresponding meta.DynamicProperty object.

  • Assign a function handle referencing your set or get property function to the
    meta.DynamicProperty object's GetMethod or SetMethod property. This function
    does not need to be a method of the class. You cannot use a naming scheme like
    set.PropertyName. Instead, use any other valid function name.


Suppose that you want to create a property set function for the myCoord dynamic
property of the button class created in “Define Dynamic Properties” on page 8-70.

Write the function as follows.

function set_myCoord(obj,val)
if ~(length(val) == 2)
error('myCoords require two values')
end
obj.myCoord = val;
end

Because button is a handle class, the property set function does not need to return the
object as an output argument.

To get the meta.DynamicProperty object, use the handle class findprop method:

Set and Get Methods for Dynamic Properties
Free download pdf