Main Content

dictionary

Object that maps unique keys to values

    Description

    A dictionary is useful for fast look up of values in a larger set. A dictionary is a map that stores data as values, which can be accessed using corresponding unique keys. Each pair of keys and values is an entry.

    Creation

    Description

    example

    d = dictionary(keys,values) creates a dictionary with specified keys and values. The resulting dictionary d is a 1-by-1 scalar object. If multiple values are assigned to the same key, then only the last of those values is assigned. New assignments to an existing key overwrite the value for that entry.

    keys and values must be the same size unless values is a scalar, where each element of keys becomes a key for values. When keys and values are arrays, the number of entries is equal to the number of key-value pairs.

    Dictionaries are typed based on their entries. All keys and all values in a dictionary must share respective data types or be able to be converted to the configured data type. If parts of a new entry do not share the configured data types then MATLAB attempt a conversion. Keys and values do not need to be of the same data type. Values of different types can be added to a dictionary if they are contained in a cell array.

    d = dictionary(k1,v1,...,kN,vN) creates a dictionary with specified key-value pairs. If multiple instances of the same key are specified then only the last key-value pair is assigned.

    d = dictionary creates an unconfigured dictionary with no keys or values.

    When a dictionary is created without inputs it is unconfigured and has no types. Adding entries to an unconfigured dictionary specifies a data type for the keys and a data type for the values.

    Input Arguments

    expand all

    Keys, specified as a scalar or an array. Individual elements of keys must be scalars of the same data type or of compatible data types.

    If keys is an array, each element creates a new key. If keys contains duplicate elements, the corresponding value of the last duplicate element is assigned.

    Values, specified as a scalar or an array. Individual elements of values must be scalars of the same data type. If values need to be heterogeneous or nonscalar, use a cell array.

    If values is an array, each element creates a new value. If keys is an array and values is a scalar, then that value is mapped to each key in keys.

    Key-value pairs, specified as separate arguments of key and value scalars and arrays. All key arguments must have the same data type or have compatible data types. All value arguments must have the same data type or have compatible data types.

    Object Functions

    entriesKey-value pairs of dictionary
    keysKeys of dictionary
    valuesValues of dictionary
    typesTypes of dictionary keys and values
    numEntriesNumber of key-value pairs in dictionary
    isConfiguredDetermine if dictionary has types assigned to keys and values
    isKeyDetermine if dictionary contains key

    Examples

    collapse all

    Create a dictionary to store the number of wheels of different vehicles.

    Create an array of names and an array of corresponding number of wheels.

    wheels = [1 2 3];
    names = ["Monocycle" "Bicycle" "Tricycle"];

    Create a dictionary using the names as keys and the number of wheels as values.

    d = dictionary(names,wheels)
    d = 
      dictionary (string ⟼ double) with 3 entries:
    
        "Monocycle" ⟼ 1
        "Bicycle"   ⟼ 2
        "Tricycle"  ⟼ 3
    
    

    Access dictionary values by using the key as an index.

    d("Tricycle")
    ans = 3
    

    Entries can be modified by assigning a new value to an existing key.

    d("Bicyle") = 2.5
    d = 
      dictionary (string ⟼ double) with 4 entries:
    
        "Monocycle" ⟼ 1
        "Bicycle"   ⟼ 2
        "Tricycle"  ⟼ 3
        "Bicyle"    ⟼ 2.5000
    
    

    Add new entries to a dictionary by assigning values to keys.

    d("Car") = 4
    d = 
      dictionary (string ⟼ double) with 5 entries:
    
        "Monocycle" ⟼ 1
        "Bicycle"   ⟼ 2
        "Tricycle"  ⟼ 3
        "Bicyle"    ⟼ 2.5000
        "Car"       ⟼ 4
    
    

    Add multiple entries using arrays of keys and values.

    names2 = ["Truck" "Motorcycle" "Sixteen-Wheeler"];
    wheels2 = [4 2 16];
    d(names2) = wheels2
    d = 
      dictionary (string ⟼ double) with 8 entries:
    
        "Monocycle"       ⟼ 1
        "Bicycle"         ⟼ 2
        "Tricycle"        ⟼ 3
        "Bicyle"          ⟼ 2.5000
        "Car"             ⟼ 4
        "Truck"           ⟼ 4
        "Motorcycle"      ⟼ 2
        "Sixteen-Wheeler" ⟼ 16
    
    

    Remove entries by assigning an empty array to an existing key.

    d("Truck") = []
    d = 
      dictionary (string ⟼ double) with 7 entries:
    
        "Monocycle"       ⟼ 1
        "Bicycle"         ⟼ 2
        "Tricycle"        ⟼ 3
        "Bicyle"          ⟼ 2.5000
        "Car"             ⟼ 4
        "Motorcycle"      ⟼ 2
        "Sixteen-Wheeler" ⟼ 16
    
    

    New entries are automatically converted to match the configured data types of the dictionary. If a conversion is not possible, MATLAB throws an error.

    d('Spider-Car') = "8"
    d = 
      dictionary (string ⟼ double) with 8 entries:
    
        "Monocycle"       ⟼ 1
        "Bicycle"         ⟼ 2
        "Tricycle"        ⟼ 3
        "Bicyle"          ⟼ 2.5000
        "Car"             ⟼ 4
        "Motorcycle"      ⟼ 2
        "Sixteen-Wheeler" ⟼ 16
        "Spider-Car"      ⟼ 8
    
    

    Dictionary values must have the same type. However, data of different types can be stored in a dictionary as a cell.

    Create a cell array containing various data types and a string array of keys.

    myValues = {datetime,@myfun,struct,[1 2 3 4]}
    myValues=1×4 cell array
        {[31-Aug-2022 03:18:58]}    {@myfun}    {1x1 struct}    {[1 2 3 4]}
    
    
    myKeys = ["my birthday" "my favorite function" "a structure" "numeric array"]
    myKeys = 1x4 string
        "my birthday"    "my favorite func..."    "a structure"    "numeric array"
    
    

    Create a dictionary using the specified keys and values.

    d = dictionary(myKeys,myValues)
    d = 
      dictionary (string ⟼ cell) with 4 entries:
    
        "my birthday"          ⟼ {[31-Aug-2022 03:18:58]}
        "my favorite function" ⟼ {@myfun}
        "a structure"          ⟼ {1x1 struct}
        "numeric array"        ⟼ {[1 2 3 4]}
    
    

    Extract values as a cell array using the values function.

    values(d)
    ans=4×1 cell array
        {[31-Aug-2022 03:18:58]}
        {                @myfun}
        {1x1 struct            }
        {[             1 2 3 4]}
    
    

    Calling dictionary without any inputs creates an unconfigured dictionary.

    Create an empty configured dictionary by specifying empty inputs of the desired types.

    d = dictionary(string([]),[])
    d = 
      dictionary (string ⟼ double) with no entries.
    
    

    This dictionary is configured and new entries can be added as long as the data types match or can be converted to the data types of the configured dictionary.

    d("Unicycle") = 1;
    d("Bicycle") = 2;
    d("Tricyle") = 3
    d = 
      dictionary (string ⟼ double) with 3 entries:
    
        "Unicycle" ⟼ 1
        "Bicycle"  ⟼ 2
        "Tricyle"  ⟼ 3
    
    

    Create an unconfigured dictionary by calling dictionary without inputs.

    d = dictionary
    d = 
      dictionary with unset key and value types.
    
    

    When you add entries to an unconfigured dictionary, MATLAB changes it to be configured.

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

    Version History

    Introduced in R2022b