python中使用matlab返回多个返回值

1.我们是可以在python中使用matlab的,也就是说能在python中调用matlab的函数。

具体的环境配置可以查阅这儿

2.一般不设置默认只返回第一个返回值,并不能返回你想要的所有返回值。

test.py的内容为:

import numpy as np

# 使用matlab2016
import matlab.engine
engine = matlab.engine.start_matlab()
results = engine.test()
print(results)

test.m的内容为:

function [a,b] = test()
    a = 1;
    b = 2;
end

运行test.py,此时,我们运行时会得到:

按理说我们应该得到[1,2],可实际上我们只得到了1。那我们在接受返回值的时候设置为

import numpy as np

# 使用matlab2016
import matlab.engine
engine = matlab.engine.start_matlab()
[a,b] = engine.test()
print(a,b)

它会报错:

 3.只需要加一个参数nargout即可,具体就是

test.py是

import numpy as np

# 使用matlab2016
import matlab.engine
engine = matlab.engine.start_matlab()
[a,b] = engine.test(nargout=2)
print(a,b)

test.m仍是

function [a,b] = test()
    a = 1;
    b = 2;
end

但是此时就能得到两个返回值了:

 加一个参数nargout就能让matlab返回多个返回值了。

哒哒哒哒~

猜你喜欢

转载自blog.csdn.net/a_cherry_blossoms/article/details/125119962