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.
Syntax
M = containers.Map(keySet,valueSet)
Usage
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).
Example
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 containers.map
object contains the following functions
functions | |
---|---|
isKey | Determine if Map object contains key |
keys | Return keys of Map object |
length | Number of key-value pairs in Map object |
remove | Delete key-value pairs from Map object |
size | Size of Map object |
values | Return 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)
.
0 Comments