matlab.metadata.Validation Class
Namespace: matlab.metadata
Description
Instances of this class contain information about property validation that is
specified in a class definition. The matlab.metadata.Validation
class
enables you to obtain information programmatically for each property in a class
definition:
Class restriction applied to the property
Size requirements of the property value
Function handles referencing validation functions applied to property values
For information on property validation, see Validate Property Values.
Properties
Methods
isValidValue
tf = isValidValue(metaValidationObj,value)
Determine if value is valid. This method returns true
if
value
is a valid value for the property whose validation is
described by metaValidationObj
.
Input Arguments
metaValidationObj
- Thematlab.metadata.Validation
object for the propertyvalue
- The potential property value to test for validity
Output Argument
true
- Value is valid for this propertyfalse
- Value is not a valid value for this property
validateValue
validateValue(metaValidationObj,value)
Test if value is valid and throw error if it is not. This method throws an error
if value
is not a valid value for the property whose validation
is described by metaValidationObj
. The error message is the same
as that thrown if the value is assigned to the property of an actual object.
Input Arguments
metaValidationObj
- Thematlab.metadata.Validation
object for the propertyvalue
- The potential property value to test for validity
Examples
The ValidationExample
class defines a property that uses several
types of validation.
classdef ValidationExample properties Prop (1,:) double {mustBeReal,mustBeGreaterThan(Prop,10)} = 200; end end
The getErrorMessage
function determines if a potential value of
Prop
is valid. If the value is not valid for one or more
reasons, the function displays an error message describing the first violation it
finds.
function getErrorMessage(possibleValue) mc = ?ValidationExample; mp = findobj(mc.PropertyList,'Name','Prop'); mv = mp.Validation; if ~mv.isValidValue(possibleValue) try mv.validateValue(possibleValue) catch errorMessage fprintf('This value is not valid because: %s\n',... errorMessage.message); end else fprintf('%d is OK\n',possibleValue) end end
For example, test a 2-by-2 matrix as a possible value.
getErrorMessage
returns an error because the input is not a
vector.
getErrorMessage([11 3; 22 50])
This value is not valid because: Value must be a vector.
Changing the input to a vector corrects this problem, but the value is still not
valid. The second element of the vector is not greater than 10, as required by the
mustBeGreaterThan
validation function.
getErrorMessage([11 3 22 50])
This value is not valid because: Value must be greater than 10.