The containers.Map of Matlab can create a data structure (or object) that maps values to unique keys. This function is very useful for storing key-value pairs and doing lookup of keys.


M = containers.Map(keySet,valueSet)


keySet specifies the keys, its type can be numeric array, cell array of character vectors or string array.

valueSet specifies the values, its type is array (numeric or cell).


The following code snippets are part of the content that I written to import variable declarations and definitions from excel tables to generate c code files.

TypeIndexPair = {'single',  7;...
                 'float',   7;...
                 'double', -7;...
                 'logical', 1;...
                 'boolean', 1;...
                 'int32',  -4;...
                 'int16',  -2;...
                 'int8',   -1;...
                 'uint32',  4;...
                 'uint16',  2;...
                 'uint8',   1};
TypeMap = containers.Map(TypeIndexPair(:,1),TypeIndexPair(:,2));

%% Test
index = TypeMap('double');

First, I write the data type and its index into a cell array TypeIndexPair. Then, take the first and second columns from the TypeIndexPair array to construct the Map data structure TypeMap. Finally, the index value corresponding to type is obtained by TypeMap('type').

Additional Information

The object contains the following functions

isKeyDetermine if Map object contains key
keysReturn keys of Map object
lengthNumber of key-value pairs in Map object
removeDelete key-value pairs from Map object
sizeSize of Map object
valuesReturn values of Map object

In addition, if you want to view the methods of an object, you can try to use the methods or methodsview function. Below is the output of methodsview(TypeMap).


Categories: Matlab


Leave a Reply

Your email address will not be published.