configureDictionary
datatypes: d = configureDictionary (keyType, valueType)
Create a dictionary with key and value types and no entries.
d = configureDictionary (keyType, valueType)
returns a configured dictionary with no entries, whose keys are of
class keyType and whose values are of class valueType, each
given as a character vector or a string scalar. The names are matched in
case, and 'char' gives 'string', since a character vector
is stored as a string scalar.
valueType may name a built-in type ('double',
'logical', 'cell', 'struct',
'function_handle', …) or any class that is not abstract.
keyType must also name a type that keyHash can hash: a
built-in type or a class with a keyHash method, such as
string, datetime, duration, calendarDuration
and categorical. MATLAB accepts any class there, a table
or a timetable among them, and refuses the key only when an entry
is added.
See also: dictionary, dictionary.types, dictionary.isConfigured, keyHash
Source Code: configureDictionary
configureDictionary creates a dictionary with key and value types and no entries. Entries added later are converted to those types.
d = configureDictionary ('string', 'double')
d = dictionary (string ⟼ double) with no entries.
d({'a', 'b'}) = int8 ([1, 2]);
values (d)
ans = 1 2
Without configured types the first entries decide them: here the values are int8. Types named beforehand avoid that, and 'char' names the string type, since text is stored as strings.
u = dictionary ();
u('a') = int8 (1);
[keyType, valueType] = types (u)
keyType = string "string" valueType = string "int8"
c = configureDictionary ('char', 'datetime');
c('start') = '2024-03-01'
c =
dictionary (string ⟼ datetime) with 1 entry:
"start" ⟼ 01-Mar-2024