将变量从python返回到Matlab
我试图将一些变量从python传递到Matlab,但没有成功。 如果我只传递一个变量,它就可以正常工作,但因为我需要传递更多不同类型的变量(矩阵、向量、标量),所以就不起作用 这是我在Python test_return.py中的代码:将变量从python返回到Matlab,python,matlab,matlab-engine,Python,Matlab,Matlab Engine,我试图将一些变量从python传递到Matlab,但没有成功。 如果我只传递一个变量,它就可以正常工作,但因为我需要传递更多不同类型的变量(矩阵、向量、标量),所以就不起作用 这是我在Python test_return.py中的代码: import numpy as np def run_test_return(): a = np.ones((5,3)) b = np.ones((10)) c = 4 return a, b, c # I d
import numpy as np
def run_test_return():
a = np.ones((5,3))
b = np.ones((10))
c = 4
return a, b, c
# I don't know if I should return the variables as tuples,list, dictionary... to be easier to read in matlab
这是要阅读的matlab脚本:
pyOut = py.importlib.import_module('test_return');
py.importlib.reload(pyOut);
[a,b,c] = py.test_return.run_test_return(); % This is the part that doesn't work, I don't know how to import more than one variable, if I import only one works fine...
a = double(py.array.array('d',py.numpy.nditer(a))); % I don't know if this is the best way to read numpy 2D array
b = double(py.array.array('d',py.numpy.nditer(a)));
c = double(py.array.array('d',py.numpy.nditer(a)));
函数返回一个元组:
>> p = py.file_return.run_test_return;
>> class(p)
py.tuple
元组中的元素具有不同的数据类型:
>> class(p{1})
py.numpy.ndarray
>> class(p{3})
py.int
在MATLAB中,您可以简单地将元组包装在一个单元格中,并使用cellfun对其进行迭代,以将每个元素转换为一个包含双精度的单元格数组:
>> c=cellfun(@double,cell(p),'UniformOutput',false);
有更新吗。。。。谢谢你的回答,但没有用。上面写着“从py.numpy.ndarray转换为double是不可能的”@JenifferBarreto你确定你试过我的建议了吗?我贴的东西对我有用。你的MATLAB版本是什么?