MATLAB Answers

Associate Map container with specific values

20 views (last 30 days)
QIAO WANG on 31 Aug 2020
Edited: per isakson on 26 Sep 2021 at 1:40
Hi seniors,
I've been struggling finding a way to associate a map container with a specific value. For instance, we have four states [5 7 19 8] as well as four map containers. Each map container is associated with one of the states. Ideally, what I want is when I'm at state 5, I can access the map it's associated with. In other words, 5 is a key to a map container. However, I'm aware that map container is not a valid value type for container.Map.
So, please suggest a good way of doing this. Appreciate it!

Answers (3)

Peter O
Peter O on 1 Sep 2020
Map containers are a type of hash object, but not all MATLAB datatypes are hashable. The keys of containers are limited to a subset of types MATLAB can guarantee will hash correctly. To use named states (which is what I think you're after), consider using an enumeration object to define the states and cast them to string to access into the map. Unfortunately, MATLAB doesn't yet seem to support enums as hash accessors directly yet
As an example
classdef WeekDays
Monday, Tuesday, Wednesday, Thursday, Friday
Then to access:
mymap = containers.Map();
mystate = WeekDays.Tuesday;
mymap(string(WeekDays.Tuesday)) = 'Data!' % Or your favorite data structure.
>>> 'Data!'
In my limited experimentation from circa 2014, Map objects can be slower to access than other structures, and the string conversion overhead of an enum object may also factor in. But give it a try! If you're not writing high performance code or something called 10,000 times, it is likely fine for your application.
  1 Comment
QIAO WANG on 1 Sep 2020
Thank you for the reply. I'll look into this suggestion and give it a try.

Sign in to comment.

Steven Lord
Steven Lord on 1 Sep 2020
As long as your states are positive integer values and the largest one isn't too large, you could make a cell array and store each of your containers.Map objects in the corresponding cell in the cell array in a similar way that I'm storing magic matrices in each of four cells in this example.
M = randperm(20, 4);
C = cell(1, 20);
for k = M
C{k} = magic(k);
  1 Comment
QIAO WANG on 1 Sep 2020
Thanks for reply. I've actually thought about this way and tried it. The only concern is that there will be empty values in the cell array as you may also see with your example. It does not seem to be the perfect solution but a good compromise.

Sign in to comment.

Mohammad Alhashash
Mohammad Alhashash on 25 Sep 2021 at 16:12
You store these Map containers into cell array of structure and create another Map container for mapping states into their corresponding index values.
Using cell array for example:
% creating 4 arbitrary Map containers
A = containers.Map ();
B = containers.Map ();
C = containers.Map ();
D = containers.Map ();
% store them into cell array container called X
X = {A, B, C, D};
% Create a mapping key/value pairs to map state into a specific index
state = containers.Map([5 7 19 8], [1 2 3 4]);
% Calling the Map container asscoiate with state 19 for example
ans = Map with properties: Count: 0 KeyType: char ValueType: any
Or you can map the state into the corresponding Map container name directly and use eval function:
% Create an new mapping function
state = containers.Map([5 7 19 8], {"A", "B", "C", "D"});
% Calling the Map container associated with state 19 for example
C = Map with properties: Count: 0 KeyType: char ValueType: any
  1 Comment
per isakson
per isakson on 26 Sep 2021 at 1:40
"However, I'm aware that map container is not a valid value type for container.Map." Isn't it? This works on my R2018b. Isn't this very close to what you asks for?
keySet = {'Jan','Feb','Mar','Apr'};
valueSet = [11,12,13,14];
M1 = containers.Map( keySet, valueSet );
valueSet = [21,22,23,24];
M2 = containers.Map( keySet, valueSet );
valueSet = [31,32,33,34];
M3 = containers.Map( keySet, valueSet );
valueSet = [41,42,43,44];
M4 = containers.Map( keySet, valueSet );
S = containers.Map( [5,7,8,19], {M1,M2,M3,M4} )
S =
Map with properties: Count: 4 KeyType: double ValueType: any
cur = S(5);
ans = 12
cur = S(19);
ans = 44
% S(8)('Jan') % throws an error

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!