Products & Services Solutions Academia Support User Community Company

Learn more about MATLAB   

isKey (Map) - Check if containers.Map contains key

Syntax

tf = isKey(M, keys)

Description

tf = isKey(M, keys) looks for the specified keys in the Map instance M, and returns logical 1 (true ) for those elements that it finds, and logical 0 (false) for those it does not. keys is a scalar key or cell array of keys. If keys is nonscalar, then return value tf is a nonscalar logical array that has the same dimensions and size as keys.

Read more about Map Containers in the MATLAB Programming Fundamentals documentation.

Examples

Construct a Map object where the keys are states in the United States and the value associated with each key is that state's capital city:

US_Capitals = containers.Map( ...
{'Arizona', 'Nebraska', 'Nevada', 'New York', ...
 'Georgia', 'Alaska', 'Vermont', 'Oregon'}, ...
{'Phoenix', 'Lincoln', 'Carson City', 'Albany', ...
 'Atlanta', 'Juneau', 'Montpelier', 'Salem'})

Check three states to see if they are in the map:

isKey(US_Capitals, {'Georgia', 'Alaska', 'Wyoming'})
ans =
     1     1     0

Check two states and a capital to see if they are all keys in the map:

isKey(US_Capitals, {'Georgia'; 'Montpelier'; 'Alaska'})
ans =
     1     0     1

Identify the capital city of a specific state, but only attempt this if you know that this state is in the map:

S = 'Nebraska';
if isKey(US_Capitals, S)
sprintf('   The capital of %s is %s', S, US_Capitals(S))
else error('The state of %s is not in the map', S)
end
ans =
   The capital of Nebraska is Lincoln

S =  Montana';
if isKey(US_Capitals, S)
sprintf('   The capital of %s is %s', S, US_Capitals(S))
else error('The state of %s is not in the map', S)
end

??? The state of Montana is not in the map

See Also

containers.Map, keys(Map), values(Map), size(Map), length(Map), remove(Map), handle

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS