MATLAB Object-Oriented Programming

(Joyce) #1

Property Validation Functions


In this section...
“Validate Property Using Functions” on page 8-40
“MATLAB Validation Functions” on page 8-43
“Define Validation Functions” on page 8-45
“Add Support for Validation Functions” on page 8-46

Validate Property Using Functions


Use property validation functions in class definitions to impose specific restrictions on
property values. A validation function accepts a potential property value as an argument
and issues an error if the value does not meet the specific requirement imposed by the
function.

During the validation process, MATLAB passes the value to each validation function listed
in the class definition. MATLAB calls each function from left to right and throws the first
error encountered. The value passed to the validation functions is the result of any
conversion applied by the class and size specifications. For more information on class and
size validation, see “Property Class and Size Validation” on page 8-31.

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

Validation Function Syntax

Specify validation functions as a comma-separated list of function names or function calls
with arguments, enclosed in braces.

classdef MyClass
properties
Prop {fcn1,fcn2,...} = defaultValue
end
end

MATLAB passes the potential property value to the validation function implicitly.
However, if the validation function requires input arguments in addition to the potential
property value, then you must include both the property and the additional arguments.
Additional arguments must be literal values and cannot reference variables. Literal values
are nonsymbolic representations, such as numbers and text.

8 Properties — Storing Class Data

Free download pdf