[matlab-to-python binding] How to pass a dictionary from matlab to python ? And loop over keys/values in python ?
Afficher commentaires plus anciens
Hi,
I am using the matlab-to-python binding (python setup.py install in <matlabroot>/extern/engines/python).
How to pass a dictionary from matlab to python ? And loop over keys/values in python ?
In matlab I have a doStuff.m file which looks like:
function dict = doStuff()
k = strings(2, 1);
v = zeros(2, 1);
for i = 1:2
v(i) = i;
k(i) = ['key', num2str(i)];
end
dict = containers.Map(k, v);
end
In python I have a doStuff.py file which looks like:
import matlab.engine
eng = matlab.engine.start_matlab()
dict = eng.doStuff()
for k in dict:
print(k, dict[k])
And finally get error of this kind in python (after matlab ran OK):
$ python doStuff.py
Traceback (most recent call last):
File "doStuff.py", line 5, in <module>
print(k, dict[k])
TypeError: sequence index must be integer, not 'matlab.object'
If this way to go could hit a limitation: is there any workaround ?
FH
Réponses (1)
Franck HOUSSEN
le 21 Mai 2019
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!