Manage Data Types in Dictionaries
Dictionary keys and values can be of almost any data type. A dictionary is configured using the data type of the keys and the data type of the values. Keys and values do not need to be of the same data type. However, all keys and all values in a dictionary must share the same respective data types or be convertible to the configured data types.
Add Entry to Configured Dictionary
If you add a new dictionary entry that does not match the configured data types for the dictionary, then MATLAB® attempts to convert the data types of the new entry to match the configured data types.
For example, create a dictionary that holds keys and values that are strings. Then add an entry with a numeric value. MATLAB converts the value to a string.
d = dictionary("hello","world")
d =
dictionary (string ⟼ string) with 1 entry:
"hello" ⟼ "world"
d("newKey") = 1d =
dictionary (string ⟼ string) with 2 entries:
"hello" ⟼ "world"
"newKey" ⟼ "1"
Store and Access Different Data Types in Dictionary
Dictionaries require that all entries share the same respective data types for keys and values. However, you can store keys (or values) of different types in a dictionary by putting them in a cell array. Each element of a cell array can contain data of any type or size. This approach satisfies the dictionary type requirement because the type of each element is cell.
For example, create a cell array containing values of various data types and then create a string array of keys.
myCell = {datetime,@myfun,struct,[1 2 3 4]}myCell=1×4 cell array
{[24-Jan-2026 22:23:47]} {@myfun} {1×1 struct} {[1 2 3 4]}
names = ["datetime now" "my favorite function" "a structure" "numeric array"]
names = 1×4 string
"datetime now" "my favorite function" "a structure" "numeric array"
Create a dictionary using the specified keys and values.
d = dictionary(names,myCell)
d =
dictionary (string ⟼ cell) with 4 entries:
"datetime now" ⟼ {[24-Jan-2026 22:23:47]}
"my favorite function" ⟼ {@myfun}
"a structure" ⟼ {1×1 struct}
"numeric array" ⟼ {[1 2 3 4]}
When the data type of the dictionary values is cell, you can directly access the values by using curly braces ({}) around the keys. (since R2023a)
For example, look up the contents of “numeric array" using curly braces.
d{"numeric array"}ans = 1×4
1 2 3 4
Similarly, you can add new entries with values of any data type to an existing dictionary by using curly braces to wrap the new values as cell. (since R2023a)
For example, add an entry with a string key and table value.
d{"a new entry"} = table([1 2;3 4])d =
dictionary (string ⟼ cell) with 5 entries:
"datetime now" ⟼ {[24-Jan-2026 22:23:47]}
"my favorite function" ⟼ {@myfun}
"a structure" ⟼ {1×1 struct}
"numeric array" ⟼ {[1 2 3 4]}
"a new entry" ⟼ {2×1 table}
Create and Access Value in Nested Dictionary
You can create a nested dictionary to organize information hierarchically. To do so, store each level of inner dictionaries as values using a cell array.
For example, create a two-level nested dictionary that stores the author and word count information for two books. Use cell arrays to accommodate the different data types for number and string in d1 and d2.
d1 = dictionary(["wordcount" "author"],{15896,"Franz Kafka"}); d2 = dictionary(["wordcount" "author"],{34962,"Albert Camus"}); dnest = dictionary(["The Metamorphosis" "The Stranger"],{d1,d2});
To access the author of The Stranger, use two levels of lookup. First, use curly braces to retrieve the inner dictionary associated with The Stranger. Then, use another pair of curly braces to access the author.
dnest{"The Stranger"}{"author"}ans = "Albert Camus"
Data Type Limitations
Dictionaries allow entries of almost any data type, but there are some limitations. Certain data types like struct are accepted as part of an entry, but vectorized operations (such as looking up multiple dictionary values simultaneously) are not supported for structures with different fields. Vectorized operations are not supported for value types that are not able to be concatenated.
Dictionaries do not accept these data types as keys or values:
Tables
Tall arrays
Distributed arrays
However, you can still use any data type as dictionary values by wrapping the values in a cell array. Wrapping keys in a cell array works for data types that are supported by keyHash and keyMatch.
See Also
dictionary | configureDictionary | cell | keyHash | keyMatch