MATLAB Object-Oriented Programming

(Joyce) #1
property must conform to the specified size or be compatible with the specified size.
For more information, see “Property Size Validation” on page 8-31.


  • Class — The name of a single MATLAB class. The value assigned to the property must
    be of the specified class or convertible to the specified class. Use any MATLAB class or
    externally defined class that is supported by MATLAB, except for Java and COM
    classes. For more information, see “Property Class Validation” on page 8-33.

  • Functions — A comma-separated list of validation function names. MATLAB passes the
    value assigned to the property to each the validation functions after applying any
    possible class and size conversions. Validator functions throw errors if the validation
    fails, but do not return values. For more information, see “Property Validation
    Functions” on page 8-40.


For a list of MATLAB validation functions, see “MATLAB Validation Functions” on page
8-43.

Using Property Validation


Use property validation for public properties to control the values user code assigns to
the properties.


If you want to restrict property values to a fixed set of identifiers, create an enumeration
class for these identifiers and constrain the property to this class. For information on
enumeration classes, see “Define Enumeration Classes” on page 14-5.


MATLAB type conversion rules apply to property validation. For example, MATLAB can
coerce among numeric types in most cases. Therefore, restricting a property value to a
specific numeric type, such as double does not prevent the assignment of other numeric
types to the property.


To ensure that a property can be assigned only a specific type of value, restrict the
property to a type that supports only the desired type conversions or use a validation
function to specify the exact class allowed for the property instead of specifying the
property type. MATLAB evaluate the type specification before executing any validation
functions. For more information, see “Order of Validation” on page 8-27.


Specify Valid Default


Ensure that any default value assigned to the property meets the restrictions imposed by
the specified validation. If you do not specify a default value, MATLAB creates a default
value by assigning an empty object of the specified class or by calling the default


Validate Property Values 8-

Free download pdf