dictionary
Object that maps unique keys to values
Since R2022b. Recommended over containers.Map
.
Description
A dictionary is useful for fast lookup 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
creates a dictionary with specified keys and values. The resulting dictionary
d
= dictionary(keys
,values
)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® attempts a conversion. Keys and values do not need to be of the same data type. Character row vectors are converted to string scalars when assigned as keys or values.
Values of different types can be added to a dictionary if they are contained in a cell
array. When performing a lookup on a dictionary that uses cells as values, a cell array is
returned. The contents of the cell array can be accessed
directly by using curly braces ({}
) instead of parentheses.
(since R2023a)
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(k1,v1,...,kN,vN
)
creates an unconfigured
dictionary with no keys or values. d
= dictionary
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
Usage
Description
Use dictionary
to create the dictionary, d
. Then
you can evaluate or change d
at specific query points using any of the
following syntaxes:
valueOut = d(keys)
looks up the value that corresponds to
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 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.
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's values are configured to be a datatype other than cell, an error is
thrown.
d{keys} = newValues
assigns cells containing the elements of
newValues
to the entries specified by the corresponding values of
keys. If the dictionary's values are configured to be a datatype other than cell an error
is thrown.
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
Version History
Introduced in R2022bSee Also
configureDictionary
| insert
| lookup
| remove
| entries
| keys
| values
| types
| numEntries
| isConfigured
| isKey
| keyHash
| keyMatch