Main Content

keys

Keys of dictionary

Since R2022b

    Description

    example

    k = keys(d) returns an array containing the keys of the specified dictionary.

    example

    k = keys(d,'cell') optionally return the keys as a cell array.

    Examples

    collapse all

    Create a dictionary containing three key-value pairs that map numbers to strings.

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

    Use keys to return an array containing the keys stored in the dictionary.

    k = keys(d)
    k = 3×1
    
         1
         2
         3
    
    

    Create a dictionary containing three key-value pairs that map numbers to strings.

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

    Use keys with the optional input "cell" to return an array containing the keys stored in the dictionary as a cell array.

    k = keys(d,"cell")
    k=3×1 cell array
        {[1]}
        {[2]}
        {[3]}
    
    

    Input Arguments

    collapse all

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

    Version History

    Introduced in R2022b