MATLAB Object-Oriented Programming

(Joyce) #1

For example, the set.Password method tests the length of the character array assigned
to a property named Password. If there are fewer than seven characters in the value
assigned to the property, MATLAB returns the error. Otherwise, MATLAB assigns the
specified value to the property.


function obj = set.Password(obj,pw)
if numel(pw) < 7
error('Password must have at least 7 characters')
else
obj.Password = pw;
end


For more information on property access methods, see “Property Access Methods” on
page 8-50.


Reference Object Properties Using Variables


MATLAB can resolve a property name from a char variable using an expression of the
form:


object.(PropertyNameVar)


where PropertyNameVar is a variable containing the name of a valid object property.
Use this syntax when passing property names as arguments. For example, the
getPropValue function returns the value of the KeyType property:


PropName = 'KeyType';
function o = getPropValue(obj,PropName)
o = obj.(PropName);
end


See Also


Related Examples



  • “Evaluation of Expressions in Class Definitions” on page 6-10

  • “Ways to Use Properties” on page 8-2

  • “Validate Property Values” on page 8-24


See Also
Free download pdf