You can call any MATLAB® function directly and return the
results to Python®. For example, to determine if a number is prime,
use the engine to call the isprime function.
import matlab.engine eng = matlab.engine.start_matlab() tf = eng.isprime(37) print(tf)
True
When you call a function with the engine, by default the engine
returns a single output argument. If you know that the function can
return multiple arguments, use the nargout argument
to specify the number of output arguments.
To determine the greatest common denominator of two numbers,
use the gcd function. Set nargout to
return the three output arguments from gcd.
import matlab.engine eng = matlab.engine.start_matlab() t = eng.gcd(100.0,80.0,nargout=3) print(t)
(20.0, 1.0, -1.0)
Some MATLAB functions return no output arguments. If the
function returns no arguments, set nargout to 0.
Open the MATLAB Help browser from Python.
import matlab.engine eng = matlab.engine.start_matlab() eng.doc(nargout=0)
The MATLAB doc function opens the browser,
but does not return output arguments. If you do not specify nargout=0,
the engine raises an error.
To stop execution of a MATLAB function press Ctrl+C. Control returns to Python.
You can use a MATLAB operator in Python by calling the equivalent function. For a list of operators and
associated function names, see MATLAB Operators and Associated Functions. For example, to add two numbers, use the
plus function instead of the +
operator.
import matlab.engine eng = matlab.engine.start_matlab() a = 2 b = 3 eng.plus(a,b)
matlab.engine.FutureResult | matlab.engine.MatlabEngine