Modifying nested structure in Matlab workspace from Python
Afficher commentaires plus anciens
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?
Réponses (1)
Chrisna Fawwaz
le 21 Déc 2022
0 votes
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)
Catégories
En savoir plus sur Call MATLAB from Python dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!