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 件)

Chrisna Fawwaz
Chrisna Fawwaz 2022 年 12 月 21 日

0 投票

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)

カテゴリ

製品

リリース

R2021b

質問済み:

2021 年 11 月 30 日

回答済み:

2022 年 12 月 21 日

Community Treasure Hunt

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

Start Hunting!

Translated by