Main Content


Determine if Map object contains key




dictionary is recommended over containers.Map because it accepts more data types as keys and values and provides better performance. (since R2022b)

TF = isKey(M,keySet) returns 1 (true) if M contains the specified key, and returns 0 (false) otherwise.

If keySet is an array that specifies multiple keys, then TF is a logical array of the same size.


collapse all

Create a Map object containing several key-value pairs.

months = {'Jan','Feb','Mar','Apr'};
rainfall = [327.2 368.2 197.6 178.4];
M = containers.Map(months,rainfall)
M = 
  Map with properties:

        Count: 4
      KeyType: char
    ValueType: double

Determine whether m has 'Feb' as a key.

TF = isKey(M,'Feb')
TF = logical

Search for multiple keys.

keySet = {'Mar','Apr','May','Jun'};
TF = isKey(M,keySet)
TF = 1x4 logical array

   1   1   0   0

Input Arguments

collapse all

Input Map object.

Keys to search for in the Map object, specified as a numeric scalar, character vector, string scalar, or cell array. To search for multiple keys, specify keySet as a cell array—even when you specify keys as numeric scalars or strings.

Version History

Introduced in R2008b