Use matlab in python to return multiple return values

1. We can use matlab in python, that is to say, we can call matlab functions in python.

The specific environment configuration can be found here .

2. Generally, if you do not set the default, only the first return value is returned, and it cannot return all the return values ​​you want.

The content of test.py is:

import numpy as np

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

The content of test.m is:

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

Run test.py, at this point, we will get:

It stands to reason that we should get [1,2], but in fact we only got 1. Then when we accept the return value, we set it to

import numpy as np

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

It will report an error:

 3. You only need to add a parameter nargout, specifically

test.py is

import numpy as np

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

test.m is still

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

But at this point you can get two return values:

 Adding a parameter nargout can make matlab return multiple return values.

da da da da ~

Guess you like

Origin blog.csdn.net/a_cherry_blossoms/article/details/125119962