Modifying nested structure in Matlab workspace from Python
Mostrar comentarios más antiguos
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?
Respuestas (1)
Chrisna Fawwaz
el 21 de Dic. de 2022
0 votos
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)
Categorías
Más información sobre Call MATLAB from Python en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!