Main Content

Examine Contents of Map

Each entry in a Map consists of two parts: a unique key and its corresponding value. To find all the keys in a Map, use the keys method. To find all of the values, use the values method.

Create a new Map called ticketMap that maps airline ticket numbers to the holders of those tickets. Construct the Map with four key/value pairs:

ticketMap = containers.Map(...
    {'2R175', 'B7398', 'A479GY', 'NZ1452'}, ...
    {'James Enright', 'Carl Haynes', 'Sarah Latham', ...
     'Bradley Reid'});

Use the keys method to display all keys in the Map. MATLAB® lists keys of type char in alphabetical order, and keys of any numeric type in numerical order:


ans = 

    '2R175'    'A479GY'    'B7398'    'NZ1452'

Next, display the values that are associated with those keys in the Map. The order of the values is determined by the order of the keys associated with them.

This table shows the keys listed in alphabetical order:

2R175James Enright
A479GYSarah Latham
B7398Carl Haynes
NZ1452Bradley Reid

The values method uses the same ordering of values:


ans = 

    'James Enright'    'Sarah Latham'    'Carl Haynes'    'Bradley Reid'

See Also

| | | | | |

Related Topics