MATLAB Mapping for Dictionaries

There are two mappings for a Slice dictionary, depending on its key type:

Key TypeValue TypeMapped TypeComments
Integral type (short, int, long), bool, byte, enum, stringAnycontainers.MapEnumeration keys must be converted to integers.
structAnystructure arraycontainers.Map does not support user-defined key types.

The mapping generates a class with the same name as the dictionary. This class provides one useful function for applications: a new function that returns an empty instance of the mapped type.

On this page:

Mapping to containers.Map

Consider the definition of our EmployeeMap once more:

Slice
dictionary<long, Employee> EmployeeMap;

Since the key is a primitive type, EmployeeMap maps to containers.Map. We can use it as shown below:

MATLAB
em = containers.Map('KeyType', 'int64', 'ValueType', 'any');
 
e = Employee();
e.number = 31;
e.firstName = 'James';
e.lastName = 'Gosling';
 
em(e.number) = e;

The value for KeyType and ValueType depends on the corresponding Slice type as shown in the following table:

Slice TypeCorresponding KeyTypeCorresponding ValueType
boolint32logical
byteint32uint8
shortint32int16
intint32int32
longint64int64
enumint32any
stringcharchar
float single
double double
any other type any


Mapping to Structure Array

A dictionary with a structure key type maps to a MATLAB structure array. This mapping sacrifices some functionality, most notably the efficient indexing provided by containers.Map, but it's a straightforward mapping that utilizes the native MATLAB structure type.

Consider this example:

Slice
struct ID
{
    string symbol;
    string exchange;
}
 
dictionary<ID, double> Quotes;

We can construct this dictionary in MATLAB as follows:

MATLAB
quotes = [];
quotes(1).key = ID('CVX', 'NYSE');
quotes(1).value = 100.00;

Each element of the structure array must have key and value fields whose types correspond to the mapped types for the Slice dictionary's key and value.

An empty dictionary with a struct key type is mapped to an empty structure array with fields named key and value.

See Also