keys

Returns the keys of a containers.Map or dictionary.

Category: Language Fundamentals > Data Types

Syntax

k = keys(M)

Returns the keys of the map M.

Description

The keys of a containers.Map always come back sorted, no matter in what order they were added. Use values(M) to get the matching values in the same order. To loop over a map, iterate over keys(M) and index with each key.

Input arguments

M
A containers.Map or a dictionary.

Output arguments

k
For a containers.Map, a 1-by-n cell array sorted by key (alphabetically for text, numerically for numbers). For a dictionary, an array of the key type in insertion order.

Example

Sorted keys of a map

m = containers.Map({'b', 'a', 'c'}, [2 1 3]);
k = keys(m)
values(m)
Output
k = 1×3 cell

    {'a'}    {'b'}    {'c'}
ans = 1×3 cell

    {[1]}    {[2]}    {[3]}

See also

values isKey containers.Map remove dictionary