MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
destination: 'Granada'
reserved: '30-Apr-2008'
origin: 'JFK'

Using ticketMap and seatingMap together, you can find the name of the person who
has reserved seat 15B:

ticket = seatingMap('15B').ticketNum;
passenger = ticketMap(ticket)

passenger =

Sarah Latham

Map to Cell Array


As with structures, you can also map to a cell array in a Map object. Continuing with the
airline example of the previous sections, some of the passengers on the flight have
“frequent flyer” accounts with the airline. Map the names of these passengers to records
of the number of miles they have used and the number of miles they still have available:

accountMap = containers.Map( ...
{'Susan Spera','Carl Haynes','Anna Latham'}, ...
{{247.5, 56.1}, {0, 1342.9}, {24.6, 314.7}});

Use the Map to retrieve account information on the passengers:

name = 'Carl Haynes';
acct = accountMap(name);

fprintf('%s has used %.1f miles on his/her account,\n', ...
name, acct{1})
fprintf(' and has %.1f miles remaining.\n', acct{2})

Carl Haynes has used 0.0 miles on his/her account,
and has 1342.9 miles remaining.

See Also
cell | containers.Map | isKey | keys | struct | values

14 Map Containers

Free download pdf