Like this?
>> m = java.util.HashMap;
>> m.put(1,'hello,world');
>> m.get(1)
ans =
hello, world
Alternatively, if you want a Matlab-native implementation, try
>> m = containers.Map;
>> m('one') = 1;
>> m('one')
ans =
1
This is actually typed - the only keys it will accept are those of type char
. You can specify the key and value type when you create the map:
>> m = containers.Map('KeyType','int32','ValueType','double');
>> m(1) = 3.14;
>> m(1)
ans =
3.14
You will now get errors if you try to put any key other than an int32
and any value other than a double
.
You also have Sets available to you:
>> s = java.util.HashSet;
>> s.put(1);
>> s.contains(1)
ans =
1
>> s.contains(2)
ans =
0