Can a Dictionary Key be a char?
Show older comments
Playing with the new dictionary in 2022b and discovered that a char can't be used as the key (can use a string though)
string.empty
dictionary(string.empty,double.empty)
char.empty
dictionary(char.empty,double.empty)
5 Comments
char(string.empty) is equal to char(zeros(0,0,0)) and not equall to char.empty, which is char(zeros(0)).
This works
d=dictionary(char(zeros(0,0,0)),[])
This is also be accepted
d=dictionary(char(zeros(0,1,0)),[])
But (oddly to me) not this
d=dictionary(char(zeros(1,1,0)),[]) % or not this either d=dictionary(char(zeros(0,1)),[])
Bruno Luong
on 17 Sep 2022
A dictionary that contains no more than 26 alphabet letters is pretty boring.
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.
Answers (3)
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'}
values = [1; 2; 3]; % A column vector of doubles.
d = dictionary(keys, values) % Create the dictionary.
% Find value when key is 'b'
val = d({'b'}) % Pass in 'b' but put it in braces to make it a cell.
See the FAQ for a good discussion of cell arrays: https://matlab.fandom.com/wiki/FAQ#What_is_a_cell_array?
2 Comments
Bruno Luong
on 18 Sep 2022
Not IMO, such detail make the doc filled with unteresting details.
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.
d=dictionary(string.empty,[])
d('a')=pi
d("a")
d('a')
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.
4 Comments
Bruno Luong
on 18 Sep 2022
Edited: Bruno Luong
on 18 Sep 2022
When enter
% d = dictionary(char.empty,double.empty)
don 't you see that there is no formal way for MATLAB to distinguish of
- the desired type key of you command is single-char key (you want) as oppose to
- non-single char key (== string), assumed.
So it is assumes you want to do non-single char key (second interpretation).
In this case char.empty is '' which is "" after convertion to string so it is consider char.empty as ONE instance and not 0 for value. Tat explaons the error messge yoy see.
This to matlab this is coherent, (and not your command):
clear d
d = dictionary(char.empty,1)
Don' try to over-interpret char.empty as meta data as you do, there is no such interpretation as empty set of char (string allows that not char array). It is just an array of char of length 0, which is one instance of char-array.
In any case the use case of single char is not useful. Beside the desire of being pedentic on the doc not many people would use single char as key for dictionary.
"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
stringofemptyc = string(emptyc)
length(stringofemptyc)
So
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]))
string(charempty)
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), [])
Categories
Find more on JSON Format 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!