Use MATLAB Dictionaries in Python
You can convert a Python®
dict to a MATLAB® dictionary, and a MATLAB dictionary to a Python
dict. In Python code, you can use dict methods on MATLAB dictionaries.
You can convert a Python
dictto a MATLAB dictionary by first passing it into thematlab.dictionaryconstructor, and then passing the generatedmatlab.dictionaryobject 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.dictionaryobject. Alternatively, you can create amatlab.dictionaryobject directly in Python. This object behaves like a Pythondict.
Convert Python Dictionary to MATLAB Dictionary or Structure
You can convert a Python
dict to a MATLAB dictionary by passing it to the Python
matlab.dictionary function and then passing the resulting
matlab.dictionary object to MATLAB. (since R2024b)
For example, create a matlab.dictionary object from a
Python
dict 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
md =
dictionary (string ⟼ double) with 3 entries:
"milk" ⟼ 3.5000
"bread" ⟼ 2.5000
"eggs" ⟼ 2.7500MATLAB 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 Python
dict to a MATLAB structure by passing the Python
dict directly to MATLAB using workspace. This conversion requires the
Python dictionary keys to be valid structure field names, as described in
struct.
eng.workspace['pd'] = pdIn MATLAB, display the structure.
pd
pd =
struct with fields:
milk: 3.5000
bread: 2.5000
eggs: 2.7500Python 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 Python
OrderedDict 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 Python
OrderedDict to a MATLAB dictionary. In MATLAB, display the dictionary.
od
od =
dictionary (string ⟼ double) with 4 entries:
"soup" ⟼ 3.5700
"bread" ⟼ 2.2900
"bacon" ⟼ 3.9100
"salad" ⟼ 5Convert 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 Python
dict by using the dict constructor.
For example, create a MATLAB dictionary and convert it to a Python
dict.
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 Python
dict.
dm = eng.workspace['dm']
dp = dict(dm)
In Python, display the dict.
dp
{'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 Python
dict methods on matlab.dictionary
objects.
For example, print the length of the matlab.dictionary object
dm using len.
len(dm)
3
These operations support matlab.dictionary objects, where
dm represents the dictionary:
list(dm)len(dm)dm[key]dm[key] = valuedel dm[key]key in dmkey not in dmiter(dm)dm.clear()dm.todict()classmethod fromkeys(iterable, value=None)dm.items()dm.keys()dm.values()dm == dm2(supported only ifdm2is amatlab.dictionaryobject; returnstrueonly ifdm2contains the same key-value pairs)
The items(), keys(), and
values() methods of a matlab.dictionary
object return a list. This behavior differs from Python
dict, whose corresponding methods return view objects.