Main Content

keys

Keys of dictionary

Since R2022b

    Description

    k = keys(d) returns an N-by-1 array of keys from the specified dictionary, where N is the number of entries in the dictionary. Keys are returned in the order in which the entries were added to the dictionary.

    example

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

    example

    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
    
    

    Loop over and display each key of the dictionary.

    for currentKey = keys(d)'
        disp("key: " + currentKey)
    end
    key: 1
    key: 2
    key: 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.

    Output Arguments

    collapse all

    Dictionary keys, returned as an N-by-1 array of keys from the specified dictionary, where N is the number of entries in the dictionary. Keys are returned in the order in which the entries were added to the dictionary.

    Extended Capabilities

    expand all

    Version History

    Introduced in R2022b

    expand all