...
Key Type | Value Type | Mapped Type | Comments |
---|
integral type,Integral type (short , int , long ), bool , byte , enum , string | Any | containers.Map | Enumeration keys must be converted to integers. |
struct | Any | Native struct structure array | containers.Map does not support user-defined key types. |
...
Since the key is a primitive type, EmployeeMap
maps to containers.Map
. We can use it as shown below:
Code Block |
---|
language | matlab |
---|
title | MATLAB |
---|
|
em = EmployeeMapcontainers.new(Map('KeyType', 'int64', 'ValueType', 'any');
e = Employee();
e.number = 31;
e.firstName = 'James';
e.lastName = 'Gosling';
em(e.number) = e; |
The new
function is especially useful for dictionaries that map to containers.Map
because this class must be constructed with the appropriate key and value types. For example, instead of calling EmployeeMap.new
, we can manually construct containers.Map
as follows:
Code Block |
---|
|
em = containers.Map('KeyType', 'int64', 'ValueType', 'any'); |
We recommend using the new
function to reduce the possibility of run-time errors.
...
The value for KeyType
and ValueType
depends on the corresponding Slice type as shown in the following table:
Slice Type | Corresponding KeyType | Corresponding ValueType |
---|
bool | int32 | logical |
byte | int32 | uint8 |
short | int32 | int16 |
int | int32 | int32 |
long | int64 | int64 |
enum | int32 | any |
string | char | char |
float | | single |
double | | double |
any other type | | any |
Mapping to Structure Array
A dictionary with a structure key type maps to a native MATLAB struct structure array. This mapping sacrifices some functionality, most notably the efficient indexing provided by containers.Map
, but it's a straightforward mapping that utilizes a the native MATLAB type that developers are likely to be familiar withstructure type.
Consider this example:
Code Block |
---|
|
struct ID
{
string symbol;
string exchange;
}
dictionary<ID, double> Quotes; |
We can construct this dictionary in MATLAB as follows:
Code Block |
---|
language | matlab |
---|
title | MATLAB |
---|
|
quotes = []; % Or call Quotes.new()
quotes(1).key = ID('CVX', 'NYSE');
quotes(1).value = 100.00; |
Each element of the struct structure array must have key
and value
fields whose types are compatible with those of 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