MATLAB Object-Oriented Programming

(Joyce) #1
a =

ImData with properties:

Data: 0

Property assignment invokes the validators in left-to-right order. Assigning a char vector
to the Data property causes an error thrown by mustBeNumeric.

a.Data = 'red'

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

Assigning a numeric value that is out of range causes an error thrown by
mustBeInRange.

a.Data = -1

Error setting property 'Data' of class 'ImData':
Value assigned to Data property is not in range 0...255

Add Support for Validation Functions


Support MATLAB validation functions for objects of your class by implementing the
dependent functions as methods of your class. To determine which methods to implement
for each function, see the validation function reference pages listed in this table
“MATLAB Validation Functions” on page 8-43.

For example, suppose that you want your class to support the mustBeGreaterThan
validation function. Overload these MATLAB functions as methods in your class:


  • isreal — Always return logical true because mustBeGreaterThan does not
    support complex numbers.

  • gt — The second object in the comparison must be scalar, as required by
    mustBeGreaterThan.


The SupportmBGT class implements support for mustBeGreaterThan.

classdef SupportmBGT
properties
Prop(1,1) double {mustBeReal}
end

8 Properties — Storing Class Data

Free download pdf