MATLAB Object-Oriented Programming

(Joyce) #1
Name Meaning Dependencies
mustBeInteger(A) A==floor(A) isreal,
isfinite, floor,
isnumeric,
islogical
mustBeMember(A,B) A is an exact match for
a member of B.

ismember

Define Validation Functions


Validator functions are ordinary MATLAB functions that are designed for the specific
purpose of validating property values. Functions used as property validators:



  • Accept the potential property value as an input argument

  • Do not return values

  • Throw errors if the validation fails


Creating your own validation function is useful when you want to provide specific
validation that is not available using the MATLAB validation functions. You can create
local functions within the class file or place the function on the MATLAB path to be
available for use in any class.


For example, the ImData class uses a local function to define a validator that restricts the
Data property to a specific range of numeric values.


classdef ImData
properties
Data {mustBeNumeric, mustBeInRange(Data,[0,255])} = 0
end
end
function mustBeInRange(a,b)
if any(a(:) < b(1)) || any(a(:) > b(2))
error(['Value assigned to Data property is not in range ',...
num2str(b(1)),'...',num2str(b(2))])
end
end


When you create an instance of the ImData class, MATLAB validates that the default
value is numeric, in the range 0...255, and not empty.


a = ImData


Property Validation Functions
Free download pdf