dictionary
Dictionary that maps unique keys to values
Since R2022b. Recommended over containers.Map.
Description
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. Use a dictionary to efficiently look up values associated with a key.
Creation
Description
creates a dictionary with specified keys and values. The resulting dictionary
d = dictionary(keys,values)d is a 1-by-1 scalar object. If you assign multiple values 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.
keys and values can be of any type, but key
types and value types must be uniform. All keys and all values in a dictionary must share
respective data types or be convertible to the configured data type. If parts of a new
entry do not share the configured data types, then MATLAB® attempts to convert the entry types. Keys and values do not need to be of
the same data type. dictionary converts keys and values specified as
character row vectors to string scalars.
To store a mix of key or value types, use a cell array. When you perform a lookup on a
dictionary that uses cells as values, the dictionary returns a cell array. You can access the contents of the cell array directly by using
curly braces {} instead of parentheses. (since R2023a)
creates a dictionary with specified key-value pairs. If you specify multiple instances of
the same key, then only the last key-value pair is assigned.d = dictionary(k1,v1,...,kN,vN)
Input Arguments
Output Arguments
Usage
Use dictionary to create the dictionary d. Then you
can access or change d using any of the following syntaxes.
Description
valueOut = d(keys) looks up the value associated with
keys.
d(keys) = newValues assigns the elements of
newValues to the entries specified by the corresponding values of
keys. If a specified key does not exist in the dictionary, then a new
entry is added. If you assign multiple values 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.
d(keys) = [] removes the entry associated with
keys from the dictionary.
valueOut = d{keys} looks up the value associated with
keys and returns the contents of the cell. If keys
is an array, a comma-separated list of the corresponding values is returned. If the
dictionary values are configured to be a data type other than cell, this syntax throws an
error.
d{keys} = newValues assigns cells containing the elements of
newValues to the entries specified by the corresponding values of
keys. If the dictionary values are configured to be a data type other than cell, this
syntax throws an error.
Object Functions
configureDictionary | Create dictionary with specified key and value types |
insert | Add entries to a dictionary |
lookup | Find value in dictionary by key |
remove | Remove dictionary entries |
entries | Key-value pairs of dictionary |
keys | Keys of dictionary |
values | Values of dictionary |
types | Types of dictionary keys and values |
numEntries | Number of key-value pairs in dictionary |
isConfigured | Determine if dictionary has types assigned to keys and values |
isKey | Determine if dictionary contains key |
Examples
Tips
You can use custom classes for both keys and values. However, if you use classes with overloaded indexing or size queries or whose behavior differs from standard array behavior, the dictionary might not behave as expected. For additional information, see Dictionaries and Custom Classes.
Entries are stored in the order that they are added and returned in the same order. For example, create a dictionary with three entries:
d = dictionary("first",1,"second",2,"third",3)
d = dictionary (string ⟼ double) with 3 entries: "first" ⟼ 1 "second" ⟼ 2 "third" ⟼ 3The keys or values from this dictionary are returned in the same order in which they were added.
k = keys(d)
k = 3×1 string array "first" "second" "third"If additional entries are added, they are added to the end of previous entries.
d("fourth") = 4d = dictionary (string ⟼ double) with 4 entries: "first" ⟼ 1 "second" ⟼ 2 "third" ⟼ 3 "fourth" ⟼ 4Updating the value of an entry does not change its place in the order of entries.