Modifying nested structure in Matlab workspace from Python

I'm trying to access a nested structure in Matlab workspace from Python using the Matlab engine for Python. I am able to read the values of nested fields but when I try to modify a nested field, it doesn't take effect.
For example, in Matlab I have a nested structure called param with a field 'a'
param.a = 20
In Python I do the following:
import matlab.engine
eng = matlab.engine.start_matlab()
eng.workspace['param']
>> {'a': 20}
eng.workspace['param']['a']
>> 20
eng.workspace['param']['a'] = 30
eng.workspace['param']['a']
>> 20
Any workspace variables that are nested cannot be changed. there is no error when I try to change them
Is there a work around for this?

回答(1 个)

I encountered the exact same problem. The work around I used is to use the eval function:
eng.eval("param.a = 30", nargout = 0)
if you want to put a python variable that has a value:
a = 30
eng.eval("param.a = {}".format(a), nargout = 0)

类别

在 帮助中心 和 File Exchange 中查找有关 Call MATLAB from Python 的更多信息

产品

版本

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by