Documentation

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

mustBeMember

Validate that value is member of specified set

Syntax

mustBeMember(A,B)

Description

example

mustBeMember(A,B) issues an error if A is not a member of the set of values specified by B. The set of values specified by A must be a case-sensitive, exact match. mustBeMember does not return a value.

When using mustBeMember as 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

Use mustBeMember to 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' is not a member 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 of Prop1 to be either 'yellow', 'green', or 'blue'.

classdef MyClass
   properties
      Prop1 {mustBeMember(Prop1,{'yellow','green','blue'})} = 'yellow'
   end
end

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' is not a member 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 using mustBeMember as 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

Set of values to which A must belong, specified as any of the following:

Example: Property with cell array of char vectors: 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?