matlab.engine.MatlabEngine - Python object using MATLAB as computational engine within Python session - MATLAB (original) (raw)

Main Content

Namespace: matlab.engine

Python object using MATLAB as computational engine within Python session

Description

The MatlabEngine class uses a MATLAB® process as a computational engine for Python®. You can call MATLAB functions as methods of a MatlabEngine object because the functions are dynamically invoked when you call them. You also can call functions and scripts that you define. You can send data to, and retrieve data from, the MATLAB workspace associated with a MatlabEngine object.

Creation

The matlab.engine.start_matlab method creates aMatlabEngine object each time it is called. There is no need to callmatlab.engine.MatlabEngine() to create MatlabEngine objects of your own.

Attributes

Attribute Description
workspace Python dictionary containing references to MATLAB variables. You can assign data to, and get data from, a MATLAB variable through the workspace. The name of each MATLAB variable you create becomes a key in the workspace dictionary. The keys in workspace must be valid MATLAB identifiers (for example, you cannot use numbers as keys).

Methods

expand all

You can call any MATLAB function as a method of a MatlabEngine object. The engine dynamically invokes a MATLAB function when you call it. The syntax shows positional, keyword, and output arguments of a function call.

ret = MatlabEngine._`matlabfunc`_(_`*args`_,nargout=1,background=False,stdout=sys.stdout,stderr=sys.stderr)

Replace _`matlabfunc`_ with the name of any MATLAB function (such as isprime or sqrt). Replace _`*args`_ with input arguments for the MATLAB function you call. The keyword arguments specify:

Specify keyword arguments only when specifying values that are not the default values shown in the syntax.

Input Arguments to MATLAB Function

Argument Description Python Type
*args Input arguments to MATLAB function, specified as positional arguments Any Python types that the engine can convert to MATLAB types

Keyword Arguments to Engine

Argument Description Python Type
nargout Number of output arguments from MATLAB function int Default: 1
background Flag to call MATLAB function asynchronouslybackground is an alias for async. However, as of Python Version 3.7, async is a keyword and cannot be used as an argument. Use the background argument instead ofasync for all supported versions of Python. bool Default: False
stdout Standard output io.StringIO object Default: sys.stdout
stderr Standard error io.StringIO object Default: sys.stderr

Output Arguments

Output Type Description Required Keyword Arguments
Python variable One output argument from MATLAB function Default values
tuple Multiple output arguments from MATLAB function nargout=n (where_n_ > 1)
None No output argument from MATLAB function nargout=0
FutureResult object A placeholder for output arguments from asynchronous call to MATLAB function background=True

Exceptions

Exception Description
MatlabExecutionError Function call fails to execute
RejectedExecutionError MATLAB engine terminated
SyntaxError Syntax error in a function call
TypeError Data type of an input or output argument not supported

Examples

collapse all

Call the MATLABsqrt function from Python using the engine.

import matlab.engine
eng = matlab.engine.start_matlab()
ret = eng.sqrt(4.0)
print(ret)

Create an array in Python and put it into the MATLAB workspace.

import matlab.engine
eng = matlab.engine.start_matlab()
px = eng.linspace(0.0,6.28,1000)

px is a MATLAB array, but eng.linspace returned it to Python. To use it in MATLAB, put the array into the MATLAB workspace.

When you add an entry to the engine workspace dictionary, you create a MATLAB variable, as well. The engine converts the data to a MATLAB data type.

Get pi from the MATLAB workspace and copy it to a Python variable.

import matlab.engine
eng = matlab.engine.start_matlab()
eng.eval('a = pi;',nargout=0)
mpi = eng.workspace['a']
print(mpi)

Version History

Introduced in R2014b