Main Content

matlab.engine.MatlabEngine

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 a MatlabEngine object each time it is called. There is no need to call matlab.engine.MatlabEngine() to create MatlabEngine objects of your own.

Attributes

AttributeDescription

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

Exceptions

ExceptionDescription
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 MATLAB sqrt function from Python using the engine.

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

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.

eng.workspace['mx'] = px

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)
3.14159265359

Version History

Introduced in R2014b