Modify Keys and Values in Map
Note Keep in mind that if you have more than one handle to a Map, modifying the handle also makes
changes to the original Map. See “Modify Copy of Map” on page 14-14, below.
Remove Keys and Values from Map
Use the remove method to delete any entries from a Map. When calling this method, specify the Map
object name and the key name to remove. MATLAB deletes the key and its associated value from the
Map.
The syntax for the remove method is
remove(mapName, 'keyname');
Start with the Map ticketMap :
ticketMap = containers.Map(...
{'2R175', 'B7398', 'A479GY', 'NZ1452'}, ...
{'James Enright', 'Carl Haynes', 'Sarah Latham', ...
'Bradley Reid'});
Remove one entry (the specified key and its value) from the Map object:
remove(ticketMap, 'NZ1452');
values(ticketMap)
ans =
'James Enright'
'Sarah Latham'
'Carl Haynes'
Modify Values
You can modify any value in a Map simply by overwriting the current value. The passenger holding
ticket A479GY is identified as Sarah Latham:
ticketMap('A479GY')
ans =
Sarah Latham
Change the passenger's first name to Anna Latham by overwriting the original value for the A479GY
key:
ticketMap('A479GY') = 'Anna Latham';
Verify the change:
ticketMap('A479GY')
ans =
Anna Latham
Modify Keys and Values in Map
14-13
Note Keep in mind that if you have more than one handle to a Map, modifying the handle also makes
changes to the original Map. See “Modify Copy of Map” on page 14-14, below.
Remove Keys and Values from Map
Use the remove method to delete any entries from a Map. When calling this method, specify the Map
object name and the key name to remove. MATLAB deletes the key and its associated value from the
Map.
The syntax for the remove method is
remove(mapName, 'keyname');
Start with the Map ticketMap :
ticketMap = containers.Map(...
{'2R175', 'B7398', 'A479GY', 'NZ1452'}, ...
{'James Enright', 'Carl Haynes', 'Sarah Latham', ...
'Bradley Reid'});
Remove one entry (the specified key and its value) from the Map object:
remove(ticketMap, 'NZ1452');
values(ticketMap)
ans =
'James Enright'
'Sarah Latham'
'Carl Haynes'
Modify Values
You can modify any value in a Map simply by overwriting the current value. The passenger holding
ticket A479GY is identified as Sarah Latham:
ticketMap('A479GY')
ans =
Sarah Latham
Change the passenger's first name to Anna Latham by overwriting the original value for the A479GY
key:
ticketMap('A479GY') = 'Anna Latham';
Verify the change:
ticketMap('A479GY')
ans =
Anna Latham
Modify Keys and Values in Map
14-13
