Use MATLAB Dictionaries in Python - MATLAB & Simulink (original) (raw)
You can convert a Python® dict
to a MATLAB® dictionary, and a MATLAB dictionary to a Pythondict
. In Python code, you can use dict
methods on MATLAB dictionaries.
- You can convert a Python
dict
to a MATLAB dictionary by first passing it into thematlab.dictionary
constructor, and then passing the generatedmatlab.dictionary
object to MATLAB. Alternatively, you can convert a Python dictionary to a MATLAB structure by passing the Python dictionary directly to MATLAB. - You can pass a MATLAB dictionary to Python as a
matlab.dictionary
object. Alternatively, you can create amatlab.dictionary
object directly in Python. This object behaves like a Pythondict
.
Convert Python Dictionary to MATLAB Dictionary or Structure
You can convert a Pythondict
to a MATLAB dictionary by passing it to the Pythonmatlab.dictionary
function and then passing the resultingmatlab.dictionary
object to MATLAB. (since R2024b)
For example, create a matlab.dictionary
object from a Pythondict
in Python. Then pass the matlab.dictionary
object to MATLAB using workspace
. In this case, MATLAB converts the keys to strings and the values to doubles.
import matlab.engine eng = matlab.engine.start_matlab('-desktop')
pd = {'milk': 3.50, 'bread': 2.50, 'eggs': 2.75} md = matlab.dictionary(pd)
eng.workspace['md'] = md
In MATLAB, display the dictionary.
md =
dictionary (string ⟼ double) with 3 entries:
"milk" ⟼ 3.5000
"bread" ⟼ 2.5000
"eggs" ⟼ 2.7500
MATLAB converts the Python keys and values to the equivalent type in MATLAB when possible. If the Python dictionary keys are not all of the same data type after being automatically converted to a MATLAB data type, MATLAB wraps the keys in cell arrays. The same is true of the Python dictionary values.
Alternatively, you can convert a Pythondict
to a MATLAB structure by passing the Pythondict
directly to MATLAB using workspace
. This conversion requires the Python dictionary keys to be valid structure field names, as described instruct.
In MATLAB, display the structure.
pd =
struct with fields:
milk: 3.5000
bread: 2.5000
eggs: 2.7500
Python object types other than dict
that support the mapping protocol can also be converted to a MATLAB dictionary. Pass these objects directly to MATLAB using workspace
. You do not need to convert such an object to a matlab.dictionary
object first. For example, convert a PythonOrderedDict
to a MATLAB dictionary.
import collections import matlab.engine eng = matlab.engine.start_matlab('-desktop')
od = collections.OrderedDict({'soup': 3.57, 'bread': 2.29, 'bacon': 3.91, 'salad': 5.00}) eng.workspace['od'] = od
MATLAB converts the PythonOrderedDict
to a MATLAB dictionary. In MATLAB, display the dictionary.
od =
dictionary (string ⟼ double) with 4 entries:
"soup" ⟼ 3.5700
"bread" ⟼ 2.2900
"bacon" ⟼ 3.9100
"salad" ⟼ 5
Convert MATLAB Dictionary to Python Dictionary
When you pass a MATLAB dictionary to Python, MATLAB passes the dictionary as a matlab.dictionary
object. The keys and values of the Python dictionary are determined by the default conversion rules for MATLAB types, which are described at Pass Data Between MATLAB and Python. You can then explicitly convert the matlab.dictionary
object to a Pythondict
by using the dict
constructor.
For example, create a MATLAB dictionary and convert it to a Pythondict
.
First, start the MATLAB engine in Python.
import matlab.engine eng = matlab.engine.start_matlab('-desktop')
Create a MATLAB dictionary in MATLAB.
dm = dictionary(["Avg Temp","Dew Point","Precipitation"],[71.12 69.07 0.0]);
Pass the MATLAB dictionary to Python. Convert the created matlab.dictionary
object into a Pythondict
.
dm = eng.workspace['dm'] dp = dict(dm)
In Python, display the dict
.
{'Avg Temp': 71.12, 'Dew Point': 69.07, 'Precipitation': 0.0}
Use Python Dictionary Methods on MATLAB Dictionaries
In Python code, you can use many Pythondict
methods on matlab.dictionary
objects.
For example, print the length of the matlab.dictionary
objectdm
using len
.
These operations support matlab.dictionary
objects, wheredm
represents the dictionary:
list(dm)
len(dm)
dm[key]
dm[key] = value
del dm[key]
key in dm
key not in dm
iter(dm)
dm.clear()
dm.todict()
_classmethod_fromkeys(_iterable_,_value=None_)
dm.items()
dm.keys()
dm.values()
dm == dm2
(supported only ifdm2
is amatlab.dictionary
object; returnstrue
only ifdm2
contains the same key-value pairs)
The items()
, keys()
, andvalues()
methods of a matlab.dictionary
object return a list. This behavior differs from Pythondict
, whose corresponding methods return view objects.