Main Content

mustBeMember

Validate that value is member of specified set

Description

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

When using mustBeMember as a property or function argument validation function, ensure that the property default value is a member of the set.

mustBeMember calls the following function to determine if value is a member of the set of values specified by S.

Class support: All numeric classes, logical, char, and MATLAB® classes that overload ismember.

example

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)
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.

This class restricts the value of the property to a specific set of values.

The value of Prop1 must be '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 validation function. 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 property 'Prop1' of class 'MyClass'. 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.

This function declares two input arguments. Input n must be a scalar, numeric value, and input typename must be either of the character vectors single or double.

function r = mbMember(n,typename)
    arguments
        n (1,1) {mustBeNumeric}
        typename {mustBeMember(typename,{'single','double'})} = 'single'
    end
    r = rand(n,typename);
end

This call to the function uses a value for typename that does not meet the requirements defined with mustBeMember and results an error.

r = mbMember(5,'int32');
Error using mbMember (line 4)
 r = mbMember(5,'int32');
                ^^^^^^^
Invalid argument at position 2. Value must be a member of this set:
    'single'
    'double'

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 | cell
Complex Number Support: Yes

Set of values to which value 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 | cell
Complex Number Support: Yes

Tips

  • mustBeMember is designed to be used for property and function argument validation.

Extended Capabilities

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

Version History

Introduced in R2017a