MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
To access the values of multiple keys, use the values method, specifying the keys in a
cell array:

values(ticketMap, {'2R175', 'B7398'})

ans =

'James Enright' 'Carl Haynes'

Map containers support scalar indexing only. You cannot use the colon operator to access
a range of keys as you can with other MATLAB classes. For example, the following
statements throw an error:

ticketMap('2R175':'B7398')
ticketMap(:)

Add Key/Value Pairs


Unlike other array types, each entry in a Map consists of two items: the value and its key.
When you write a new value to a Map, you must supply its key as well. This key must be
consistent in type with any other keys in the Map.

Use the following syntax to insert additional elements into a Map:

existingMapObj(newKeyName) = newValue;

Start with the Map ticketMap :

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

Add two more entries to the ticketMap Map. Verify that ticketMap now has six key/
value pairs:

ticketMap('947F4') = 'Susan Spera';
ticketMap('417R93') = 'Patricia Hughes';

ticketMap.Count

ans =

6

14 Map Containers

Free download pdf