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.
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:
val = myGet(obj)
Obtain the dynamic property's corresponding
Assign a function handle referencing your set or get property function to the
SetMethod property. This function does not need to be a method of the class. You cannot use a naming scheme like
set.. 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.
Write the function as follows.
function set_myCoord(obj,val) if ~(length(val) == 2) error('myCoords require two values') end obj.myCoord = val; end
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
mb1 = b1.findprop('myCoord'); mb1.SetMethod = @set_myCoord;
MATLAB® calls the property set function whenever you set this property:
b1.myCoord = [1 2 3] % length must be two
Error using button.set_myCoord myCoords require two values
You can set and get the property values only from within your property access methods. You cannot call another function from the set or get method, and then attempt to access the property value from that function.
You can assign the same function handle for the set or get method of multiple dynamic properties. MATLAB passes only the object and the value to the assigned set function.
Reference or assignment to a property from within its set or get method does not invoke the set or get method again. Therefore, if you use a handle to the same function for multiple dynamic properties, that function is not invoked when accessing any of those properties from within that function.