MATLAB Object-Oriented Programming

(Joyce) #1

For example, consider the function mustBeGreaterThan. It requires a limiting value as
an input parameter. This validation function requires that a property value must be
greater than this limiting value.


Pass the property as the first argument. Use the property name, but do not enclose the
name in quotation marks. This property definition restricts Prop to values greater than
10.


properties
Prop {mustBeGreaterThan(Prop,10)}
end


Using Validation Functions


The following class specifies validation functions for each property.



  • Data must be numeric and finite.

  • Interp must be one of the three options listed. Specify a default value for this
    property to satisfy this requirement.


classdef ValidatorFunction
properties
Data {mustBeNumeric, mustBeFinite}
Interp {mustBeMember(Interp,{'linear','cubic','spline'})} = 'linear'
end
end


Creating a default object of the class shows the initial values.


a = ValidatorFunction


a =


ValidatorFunction with properties:


Data: []
Interp: 'linear'


Assigning values to properties calls the validation functions.


a.Data = 'cubic'


Error setting property 'Data' of class 'ValidatorFunction':
Value must be numeric.


Because the Data property validation does not include a numeric class, there is no
conversion of the char vector to a numeric value. If you change the validation of the


Property Validation Functions......................... 8-

Free download pdf