MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

List all of the keys and values in ticketMap:


keys(ticketMap), values(ticketMap)


ans =


'2R175' '417R93' '947F4' 'A479GY' 'B7398' 'NZ1452'


ans =


'James Enright' 'Patricia Hughes' 'Susan Spera' 'Sarah Latham' 'Carl Haynes' 'Bradley Reid'


Build Map with Concatenation


You can add key/value pairs to a Map in groups using concatenation. The concatenation of
Map objects is different from other classes. Instead of building a vector of Map objects,
MATLAB returns a single Map containing the key/value pairs from each of the
contributing Map objects.


Rules for the concatenation of Map objects are:



  • Only vertical vectors of Map objects are allowed. You cannot create an m-by-n array or
    a horizontal vector of Map objects. For this reason, vertcat is supported for Map
    objects, but not horzcat.

  • All keys in each Map being concatenated must be of the same class.

  • You can combine Maps with different numbers of key/value pairs. The result is a single
    Map object containing key/value pairs from each of the contributing Map objects:


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

tMap2 = containers.Map({'417R93', 'NZ1452', '947F4'}, ...
{'Patricia Hughes', 'Bradley Reid', 'Susan Spera'});

% Concatenate the two maps:
ticketMap = [tMap1; tMap2];

The result of this concatenation is the same 6-element Map that was constructed in
the previous section:
ticketMap.Count
ans =
6
keys(ticketMap), values(ticketMap)

Read and Write Using Key Index
Free download pdf