MATLAB Object-Oriented Programming

(Joyce) #1

methods
function set.PropertyName(obj,value)
...
end


Use default method attributes for property set methods. The methods block defining the
set method cannot specify attributes.


Validate Property Set Value


Use the property set method to validate the value being assigned to the property. The
property set method can perform actions like error checking on the input value before
taking whatever action is necessary to store the new property value.


classdef MyClass
properties
Prop1
end
methods
function obj = set.Prop1(obj,value)
if (value > 0)
obj.Prop1 = value;
else
error('Property value must be positive')
end
end
end
end


For an example of a property set method, see “Restrict Properties to Specific Values” on
page 3-21.


When Set Method Is Called


If a property set method exists, MATLAB calls it whenever a value is assigned to that
property. However, MATLAB does NOT call property set methods in the following cases:



  • A value is assigned to a property from within its own property set method, to prevent
    recursive calling of the set method. However, property assignments made from
    functions called by a set method do call the set method.

  • MATLAB assigns a default value to the property during initialization of an object
    before calling object constructor functions.


Property Set Methods................................ 8-

Free download pdf