matlab.metadata.Validation - Describes property validation - MATLAB (original) (raw)

Main Content

Namespace: matlab.metadata

Describes property validation

Renamed from meta.Validation in R2024a

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:

For information on property validation, see Validate Property Values.

Properties

expand all

Class restriction applied to property, specified as amatlab.metadata.Class object. If the property definition does not contain a class restriction, MATLABĀ® sets this property to a 0-by-0matlab.metadata.Class array.

Attributes:

GetAccess public
SetAccess private

Dimensions of the property value, specified as a heterogeneous array of type matlab.metadata.ArrayDimension or arrays of typematlab.metadata.FixedDimension ormatlab.metadata.UnrestrictedDimension. If the property definition does not specify dimensions for the property, MATLAB sets this property to a 1-by-0matlab.metadata.ArrayDimension array.

Attributes:

GetAccess public
SetAccess private

Validation functions, specified as a cell array of function handles referencing each validation function. If the property does not use validation functions, MATLAB sets this property to a 1-by-0 cell array.

Attributes:

GetAccess public
SetAccess private

Methods

isValidValue

tf = isValidValue(metaValidationObj,value)

Determine if value is valid. This method returns true ifvalue is a valid value for the property whose validation is described by metaValidationObj.

Input Arguments

Output Argument

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

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 ofProp 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 themustBeGreaterThan validation function.

getErrorMessage([11 3 22 50])

This value is not valid because: Value must be greater than 10.

Version History

Introduced in R2018a

expand all

The namespace of Validation has been changed frommeta to matlab.metadata. The behavior remains the same.

MATLAB will continue to recognize the old metaclass names in most contexts. However, code that relies on string comparisons to identify metaclasses might need to be updated to continue to work as expected. For example, if mObj in the example below is a matlab.metadata.Validation instance, the code under the case statement for'meta.Validation' does not execute becauseclass(mObj) returns'matlab.metadata.Validation'.

switch class(mObj) case 'meta.Validation' % code to execute if mObj is a meta.Validation instance ... end

To ensure this code continues to work as intended, use an if statement with isa. The isa command recognizes both the old and new names of the class.

if isa(mObj,'meta.Validation) % code to execute if mObj is a % matlab.metadata.Validation instance else ... end

If compatibility with older releases of MATLAB is not a concern, you can update 'meta.Validation to 'matlab.metadata.Validation'. However, continue to use the old name if your code needs to run on versions of MATLAB before R2024a.