[matlab-to-python binding] How to pass a dictionary from matlab to python ? And loop over keys/values in python ?
显示 更早的评论
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
回答(1 个)
类别
在 帮助中心 和 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!