Documentation

mustBeMember

Validate that value is member of specified set

Syntax

mustBeMember(A,B)

Description

example

mustBeMember(A,B)issues an error ifA不是我mber of the set of values specified byB. The set of values specified byAmust be a case-sensitive, exact match.mustBeMemberdoes not return a value.

When usingmustBeMemberas a property validation function, ensure that the property default value is a member of the set.

This function accepts user-defined objects if the class of the object implements the following method:

Examples

collapse all

UsemustBeMemberto validate that the first input is a member of the set of values specified by the second input.

Validate that the character vector'red'is a member of set of character vectors,'yellow','green', and'blue'.

A ='red'; B = {'yellow','green','blue'}; mustBeMember(A,B)
Error using mustBeMember (line 14) Value must be a member of this set 'yellow' 'green' 'blue'

The validation failed because'red'不是我mber of the set. MATLAB®returns an error message listing the allowed values.

Constrain property values to a specific set of values.

This class constrains the value ofProp1to be either'yellow','green', or'blue'.

classdefMyClasspropertiesProp1 {mustBeMember(Prop1,{'yellow','green','blue'})} ='yellow'endend

The default property value must comply with the restrictions imposed by the validator. Therefore, you must explicitly assign a default value that is a member of the set.

Create an object and assign a value to its property.

obj = MyClass obj.Prop1 ='red';
Error setting 'Prop1' property of 'MyClass' class: Value must be a member of this set 'yellow' 'green' 'blue'

The validation failed because'red'不是我mber of the set. MATLAB returns an error message listing the allowed values.

Input Arguments

collapse all

Value to validate, specified as a scalar or an array of one of the following:

When usingmustBeMemberas a property validator, this argument must be the property name, specified without quotation marks.

Example:PropName {mustBeMember(PropName,{'High','Medium','Low'})} = 'Low'

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical|char|string
Complex Number Support:Yes

设置的值Amust belong, specified as any of the following:

Example:Property with cell array ofcharvectors:PropName {mustBeMember(PropName,{'yellow','green','blue'})} = 'blue'

Example:Property with string array:PropName {mustBeMember(PropName,["yellow","green","blue"])} = "blue"

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|logical|char|string
Complex Number Support:Yes

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Introduced in R2017a

Was this topic helpful?