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
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)
类别
在 帮助中心 和 File Exchange 中查找有关 Call MATLAB from Python 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!