Main Content


Determine if dictionary contains key

Since R2022b



    tf = isKey(d,key) returns a logical 1 (true) if the configured dictionary contains the specified key, and it returns a logical 0 (false) otherwise. If d is an unconfigured dictionary, isKey throws an error.

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


    collapse all

    Create a dictionary containing several key-value pairs.

    names = ["Unicycle" "Bicycle" "Tricycle"];
    wheels = [1 2 3];
    d = dictionary(wheels,names)
    d =
      dictionary (double --> string) with 3 entries:
        1 --> "Unicycle"
        2 --> "Bicycle"
        3 --> "Tricycle"

    Determine whether the dictionary has 1 as a key.

    tf = isKey(d,1)
    tf = logical

    Search for multiple keys.

    key = [1 2 3 4 5];
    tf = isKey(d,key)
    tf = 1x5 logical array
       1   1   1   0   0

    Input Arguments

    collapse all

    Dictionary, specified as a dictionary object. If d is unconfigured, isKey throws an error.

    Key set, specified as an array. The data type of key must match or be convertible to the data type of keys in d.

    Version History

    Introduced in R2022b