containers.Map

Map object that stores values under keys of text or numbers.

Category: Language Fundamentals > Data Types

Syntax

M = containers.Map()

Creates an empty map with character keys and any values.

M = containers.Map(keys, values)

Creates a map from a cell array of keys and matching values.

M = containers.Map('KeyType', kt, 'ValueType', vt)

Creates an empty map with the given key and value types.

Description

A map is a handle object: assigning it to another variable does not copy it. Read with M(key) and write with M(key) = value; reading a missing key is an error. isKey(M, key), keys(M), values(M), remove(M, key) and the property M.Count manage the contents.

Keys are always kept in sorted order, so keys(M) is alphabetical for text keys.

Input arguments

keys
Cell array of keys (all text or all numeric), or one key.
values
Cell array or numeric array of values, same count as keys.
'KeyType'
'char' (default), 'double', 'single', 'int32', 'uint32', and so on.
'ValueType'
'any' (default), 'double', 'char', 'logical', and so on.

Output arguments

M
A handle to the map object.

Example

Create, read and query

m = containers.Map({'a','b'}, [10 20]);
m('c') = 30;
m('b')
isKey(m, 'z')
m.keys
m.Count
Output
ans = 20
ans = logical
   0
ans = 1×3 cell

    {'a'}    {'b'}    {'c'}
ans = uint64
   3

See also

dictionary isKey keys values struct