containers.Map — Create key-value dictionary objects in MATLAB and RunMat.
containers.Map creates host-resident handle objects that associate unique character or real numeric keys with values. Public key types are char, double, single, int32, uint32, int64, and uint64; declared value types include any, char, logical, both floating classes, and all eight integer classes.
Syntax
M = containers.Map()
M = containers.Map(keys, values)
M = containers.Map(keys, values, 'UniformValues', isUniform)
M = containers.Map('KeyType', kType, 'ValueType', vType)Inputs
keys- Type
Any- Required
- Yes
- Description
- Key container (cell, string/char, or numeric vector).
values- Type
Any- Required
- Yes
- Description
- Value container aligned with keys.
UniformValues- Type
StringScalar- Required
- Yes
- Description
- Literal UniformValues option name.
isUniform- Type
LogicalArray- Required
- Yes
- Description
- Logical scalar selecting uniform-value validation.
options- Type
Any- Required
- Variadic
- Description
- The required KeyType and ValueType name/value pairs, in either order.
Returns
M- Type
Any- Description
- containers.Map handle object.
Errors
RunMat:containers.Map:InvalidArgument- When
- Map constructor/method inputs, option grammar, or key/value payloads are invalid.
- Message
containers.Map: invalid argument
RunMat:containers.Map:MissingKey- When
- Lookup/removal targets a key that is not present in the map.
- Message
containers.Map: The specified key is not present in this container.
RunMat:containers.Map:Internal- When
- Map registry/storage operations fail unexpectedly.
- Message
containers.Map: internal operation failed
Behavior
Constructor keys must be unique and duplicates raise an error. Later assignment to an existing key overwrites the stored value.
The read-only KeyType, ValueType, and Count properties are readable with dot-indexing; Count is a uint64 scalar.
map(key) returns the associated value; requesting a missing key raises the MATLAB-compatible error *"The specified key is not present in this container."*
Assignments of the form map(key) = value update or insert entries.
RunMat returns keys(map) as a sorted row cell and aligns values(map) to that order, matching public examples without treating ordering as a formal MATLAB guarantee. values(map,keySet) requires a cell key set and preserves its shape.
isKey returns scalar logical for one scalar key and a same-shape logical result for key collections; remove mutates the handle and has no public output.
Integer key identity is exact for int32, uint32, int64, and uint64, including values wider than binary64 can distinguish. Narrow integer key arrays reject rather than alias through double.
All eight integer value classes preserve native storage through construction, assignment, lookup, and values output. Declared integer ValueType values are scalar and use the runtime's MATLAB integer saturation rules; ValueType any retains nonscalar integer arrays exactly.
UniformValues defaults to true for populated constructors; specifying false permits heterogeneous values and sets ValueType to any.
MATLAB documents thread-based execution but no gpuArray capability for this class. RunMat resident input gathering is therefore compatibility-gated and always produces host Map state and host outputs.
Execution and placement
Map storage and outputs are host-resident. Resident constructor, key-query, removal, lookup, selected-values, and assignment inputs are gated RunMat extensions that gather before host Map processing.
Map handles are local mutable identities whose entries are owned and traced by the runtime's garbage collector. Copying a handle aliases the same map; it does not transfer the map to another worker.
The map is a host-side fusion boundary. No GPU elementwise or reduction kernel is used.
Examples
Create an empty map with default types
m = containers.Map();Build a map from paired cell arrays
k = {'apple','pear','banana'};
v = {42,[1 2 3],true};
fruit = containers.Map(k,v,'UniformValues',false);Update an existing key and add a new one
fruit = containers.Map({'apple'},{42},'UniformValues',false);
fruit('apple') = 99;
fruit('peach') = struct('ripe',true);Query keys, values, and membership
fruit = containers.Map({'apple','pear','banana'},{99,[1 2 3],true},'UniformValues',false);
fruit('peach') = struct('ripe',true);
k = keys(fruit);
v = values(fruit);
tf = isKey(fruit,{'apple','durian'});Remove keys and inspect the map length
fruit = containers.Map({'apple','pear','banana','peach'},{1,2,3,4});
remove(fruit,{'pear','banana'});Using containers.Map with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how containers.Map changes the result.
Run a small containers.Map example, explain the result, then change one input and compare the output.
FAQ
Which key types are supported?⌄
Public KeyType values are 'char', 'double', 'single', 'int32', 'uint32', 'int64', and 'uint64'. String-array constructor keys are converted to character-vector keys, so the resulting KeyType is 'char'; logical and narrow integer KeyType values are not public Map key types.
What happens when I provide duplicate keys at construction time?⌄
Duplicate keys raise the same error as MATLAB: *"Duplicate key name was provided."* During assignment, duplicate keys overwrite the existing value.
Does RunMat honour 'UniformValues', true?⌄
Yes. When this option is set, RunMat enforces that each inserted value matches the MATLAB class of the first value. Retrieval still uses cell arrays, mirroring MATLAB when 'ValueType' is 'any'.
Can I store GPU arrays as map values?⌄
Only as a RunMat extension. MATLAB does not document gpuArray support for containers.Map. With RunMat extensions enabled, resident keys or values gather before processing and the Map stores host values; strict compatibility mode rejects before provider access.
How does length(map) behave?⌄
length(map) returns the number of stored keys (identical to the Count property). size(map) remains [1 1], matching MATLAB's handle semantics.
What error is raised when a key is missing?⌄
Indexing a missing key produces the MATLAB-compatible error message *"The specified key is not present in this container."*
Does the map preserve insertion order?⌄
No insertion-order guarantee should be assumed. RunMat returns keys in key order and aligns values(map) to that order, matching the ordering shown by MATLAB's public examples.
How do I remove every entry?⌄
Call remove(map, keys(map)) to empty the existing map, including any aliases to it. Assigning a new empty map changes only that variable's handle. Unreachable map storage is reclaimed by the runtime's garbage collector.
What happens if I pass a non-scalar key?⌄
Keys must be scalar. Passing vectors, matrices, or nested cell arrays of keys raises a descriptive error pointing to the offending argument.
How do I create an empty containers.Map?⌄
— Call the constructor with no arguments: m = containers.Map(). This yields an empty map with the default 'char' KeyType and 'any' ValueType. To pin the types up front (so the first insertion can't change them), supply them explicitly: m = containers.Map('KeyType', 'char', 'ValueType', 'double'). Either form gives you a map you can populate via m(key) = value.
What is the default KeyType and ValueType?⌄
— The default KeyType is 'char' and the default ValueType is 'any'. That is why containers.Map() with no arguments accepts character-vector keys and arbitrary values. To use numeric keys (for example double or int32) or to restrict values to a specific class, set 'KeyType'/'ValueType' at construction time — they become read-only properties afterwards.
Can I use MATLAB string values as containers.Map keys?⌄
String arrays are accepted as constructor key sets and converted to character vectors, making the resulting KeyType 'char'. Key lookup methods also accept a string scalar as a textual key, but 'string' is not a KeyType value.
Is containers.Map a handle class?⌄
— Yes. containers.Map inherits from handle, so maps have reference semantics: if you assign m2 = m1, both variables point to the same underlying map and mutations through either handle are visible through the other. To get an independent copy, rebuild the map from keys(m1) and values(m1) (for example, m2 = containers.Map(keys(m1), values(m1))). MATLAB does not expose a public copy method on containers.Map.
Related Containers functions
containers.Map.isKey · containers.Map.keys · containers.Map.remove · containers.Map.subsasgn · containers.Map.subsref · containers.Map.values
Source you can read
Every RunMat function's source is public. Read exactly how containers.Map is executed, line by line, in Rust.
- View the source for containers.Map in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.