Validate Property and Input Values
This example shows how to verify that the inputs and property values given to your System object™ are valid.
Validate a Single Property
To validate a property value, independent of other properties, use MATLAB class property validation. This example shows how to specify a logical property, a positive integer property, and a string property that must be one of three values.
properties UseIncrement (1,1) logical = false WrapValue (1,1) {mustBePositive, mustBeInteger} = 1 Color (1,1) string {mustBeMember(Color, ["red","green","blue"])} = "red" end
Validate Interdependent Properties
To validate the values of two or more interdependent properties, use the validatePropertiesImpl
. This example shows how to write
validatePropertiesImpl
to verify that a logical property
(UseIncrement
) is true
and the value of
WrapValue
is larger than Increment
.
methods (Access = protected) function validatePropertiesImpl(obj) if obj.UseIncrement && obj.WrapValue > obj.Increment error("Wrap value must be less than increment value"); end end end
Validate Inputs
To validate input values, use the validateInputsImpl
method. This example shows how to validate that the first
input is a numeric value.
methods (Access = protected) function validateInputsImpl(~,x) if ~isnumeric(x) error("Input must be numeric"); end end end
Complete Class Example
This example is a complete System object that shows examples of each type of validation syntax.
classdef AddOne < matlab.System % ADDONE Compute an output value by incrementing the input value % All properties occur inside a properties declaration. % These properties have public access (the default) properties UseIncrement (1,1) logical = false WrapValue (1,1) {mustBePositive, mustBeInteger} = 10 Increment (1,1) {mustBePositive, mustBeInteger} = 1 end methods (Access = protected) function validatePropertiesImpl(obj) if obj.UseIncrement && obj.WrapValue > obj.Increment error("Wrap value must be less than increment value"); end end % Validate the inputs to the object function validateInputsImpl(~,x) if ~isnumeric(x) error("Input must be numeric"); end end function out = stepImpl(obj,in) if obj.UseIncrement out = in + obj.Increment; else out = in + 1; end end end end
See Also
validateInputsImpl
| validatePropertiesImpl