Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB: extending value list in container.Map object

I have been reading over the documentation for using Matlab's container.Map to build something similar to a Python dictionary, but am running into some issues and was hoping someone could shed some light.

Is there a way to extend the contents of the values list that is mapped to a certain key? for instance, say for map "map", key "1234" I have a value of "1.0".

map(1234) = 1.0

I would like to extend the values list to [1.0 2.0], and the way I try to do this is

map = containers.Map(1234,1.0)
map(1234) = [map(1234) 2.0]

but I receive an error saying "Error using containers.Map/subsasgn. Specified value type does not match the type expected for this container."

Can I not associate arrays as values to a map key?

Many thanks!

like image 861
JoeMcG Avatar asked Jan 31 '12 19:01

JoeMcG


1 Answers

Nonscalar arrays are supported fine if the ValueType is 'any'. Looks like the problem isn't the technique you're using to extend, but the key or value type.

When you use the constructor that takes a key and a value, it infers the key and value type from the values passed in. If the value is scalar double, it infers the type to be 'double'. That doesn't support nonscalar arrays.

>> map = containers.Map(1234, 1.0);
>> disp(map.ValueType)
double

The default containers.Map constructor will have KeyType 'char' and ValueType 'any'. That will have the extending behavior you want, but you'd have to use char keys instead. Use the constructor form to explicitly set the key and value types to 'double' and 'any', and it'll work the way you want.

map = containers.Map('KeyType','double', 'ValueType','any');
map(1234) = 1.0;
map(1234) = [map(1234) 2.0];
like image 97
Andrew Janke Avatar answered Nov 09 '22 15:11

Andrew Janke