Can a Dictionary Key be a char?

22 views (last 30 days)
Paul on 17 Sep 2022
Edited: Bruno Luong on 18 Sep 2022
Playing with the new dictionary in 2022b and discovered that a char can't be used as the key (can use a string though)
ans = 0×0 empty string array
ans =
dictionary (stringdouble) with no entries.
ans = 0×0 empty char array
Error using dictionary
Dimensions of the key and value must be compatible, or the value must be scalar.
Bruno Luong
Bruno Luong on 18 Sep 2022
Edited: Bruno Luong on 18 Sep 2022
"Except that the first two examples make a dictionary with key type string"
Does it matter for user? It is string internally only, but user can still add, inquire delete using char key with such dictionary.
TMW makes great effort to make many function works for both string and char-array. So internally of dictionaly class the constructor converts char-key to string, so the method of the class can be single branch. I state it clearly in my answer.
char array is historical data type (*) and has simplicity but lack of formal sophisticated object property as string. At your level I'm still surprise you bump into the difficulty to understand the difference between char.empty and string.empty.
(*) it hacks the second dimension to store "string" or must be contains in cell which is another top level totally different.

Sign in to comment.

Answers (3)

Image Analyst
Image Analyst on 17 Sep 2022
You have to put the character or character array into a cell because the keys are in a cell array. Watch:
keys = {'a'; 'b'; 'c'}
keys = 3×1 cell array
{'a'} {'b'} {'c'}
values = [1; 2; 3]; % A column vector of doubles.
d = dictionary(keys, values) % Create the dictionary.
d =
dictionary (celldouble) with 3 entries: {'a'} ⟼ 1 {'b'} ⟼ 2 {'c'} ⟼ 3
% Find value when key is 'b'
val = d({'b'}) % Pass in 'b' but put it in braces to make it a cell.
val = 2
See the FAQ for a good discussion of cell arrays:
Bruno Luong
Bruno Luong on 18 Sep 2022
"n any case, if a char key is not allowed, dictionary should say so, IMO."
Not IMO, such detail make the doc filled with unteresting details.

Sign in to comment.

Bruno Luong
Bruno Luong on 17 Sep 2022
Edited: Bruno Luong on 17 Sep 2022
It looks to me that dictionary can accept both char array and string indisrtinctively as keys, as it convert to string internally, but one can add, inquire key in either format. >So it is justt flexible kind of interface.
Nothing prevent you to use exclusively char array of length-1 (so a char) as key. Internally it will associate or equivalent to string array of length-1 strings.
Bruno Luong
Bruno Luong on 18 Sep 2022
Edited: Bruno Luong on 18 Sep 2022
"Why wouldn't there be way for Matalb to distinguish those two cases? "
Obviousmy because they want the two types to be interchagable as much as possible, if you have followed they deveoptment of string and char. You might not be happy but it's their line of development.
Explanation of "one instance"
emptyc = char.empty
emptyc = 0×0 empty char array
stringofemptyc = string(emptyc)
stringofemptyc = ""
ans = 1
the equivalent of emptyc in string is NOT string.empty but "". This object is one instance (scalar if you will) of class string.
There is NO strict equivalent of char-array of string.empty, or put it that way, it's NOT char.empty but
charempty = char(zeros([0 0 0]))
charempty = 0×0×0 empty char array
ans = 0×0 empty string array

Sign in to comment.

Bruno Luong
Bruno Luong on 18 Sep 2022
Edited: Bruno Luong on 18 Sep 2022
The syntax that needs to be used is
d = dictionary(char.empty(0,0,0), [])
d =
dictionary (stringdouble) with no entries.


Find more on Historical Contests in Help Center and File Exchange





Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!