Create Map Object
A Map is an object of the Map class. It is defined within a MATLAB package called containers. As
with any class, you use its constructor function to create any new instances of it. You must include the
package name when calling the constructor:
newMap = containers.Map(optional_keys_and_values)
Construct Empty Map Object
When you call the Map constructor with no input arguments, MATLAB constructs an empty Map
object. When you do not end the command with a semicolon, MATLAB displays the following
information about the object you have constructed:
newMap = containers.Map
newMap =
Map with properties:
Count: 0
KeyType: char
ValueType: any
The properties of an empty Map object are set to their default values:
• Count = 0
• KeyType = 'char'
• ValueType = 'any'
Once you construct the empty Map object, you can use the keys and values methods to populate it.
For a summary of MATLAB functions you can use with a Map object, see “Methods of Map Class” on
page 14-4
Construct Initialized Map Object
Most of the time, you will want to initialize the Map with at least some keys and values at the time
you construct it. You can enter one or more sets of keys and values using the syntax shown here. The
brace operators ({}) are not required if you enter only one key/value pair:
mapObj = containers.Map({key1, key2, ...}, {val1, val2, ...});
For those keys and values that are character vectors, be sure that you specify them enclosed within
single quotation marks. For example, when constructing a Map that has character vectors as keys,
use
mapObj = containers.Map(...
{'keystr1', 'keystr2', ...}, {val1, val2, ...});
As an example of constructing an initialized Map object, create a new Map for the following key/value
pairs taken from the monthly rainfall map shown earlier in this section.
14 Map Containers
14-6
Précédent

- 574/1450

Suivant