Python - matlab engine example

Main Content

This example shows how to add variables to the MATLAB® engine workspace in Python®.

When you start the engine, it provides an interface to a collection of all MATLAB variables. This collection, named workspace, is implemented as a Python dictionary that is attached to the engine. The name of each MATLAB variable becomes a key in the workspace dictionary. The keys in workspace must be valid MATLAB identifiers (e.g., you cannot use numbers as keys). You can add variables to the engine workspace in Python, and then you can use the variables in MATLAB functions.

Add a variable to the engine workspace.

import matlab.engine
eng = matlab.engine.start_matlab()
x = 4.0
eng.workspace['y'] = x
a = eng.eval('sqrt(y)')
print(a)

In this example, x exists only as a Python variable. Its value is assigned to a new entry in the engine workspace, called y, creating a MATLAB variable. You can then call the MATLAB eval function to execute the sqrt(y) statement in MATLAB and return the output value, 2.0, to Python.

See Also

matlab.engine.MatlabEngine | matlab.engine.FutureResult

  • Call MATLAB Functions from Python
  • Sort and Plot MATLAB Data from Python

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.stsdout,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:

  • The number of output arguments the function returns

  • Whether the engine calls the function asynchronously

  • Where the engine sends standard output and standard error coming from the function

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

Input Arguments to MATLAB Function

ArgumentDescriptionPython 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

ArgumentDescriptionPython Type

nargout

Number of output arguments from MATLAB function

int

Default: 1

background

Flag to call MATLAB function asynchronously

background 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 of async for all supported versions of Python.

bool

Default: False

stdout

Standard output

StringIO.StringIO object (Python 2.7)
io.StringIO object (Python 3.x)

Default: sys.stdout

stderr

Standard error

StringIO.StringIO object (Python 2.7)
io.StringIO object (Python 3.x)

Default: sys.stderr

Output Arguments

Output TypeDescriptionRequired 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

How do I run MATLAB engine in Python?

Start MATLAB Engine for Python.
Start Python® at the operating system prompt..
Import the matlab. engine package into your Python session..
Start a new MATLAB® process by calling start_matlab . The start_matlab function returns a Python object eng which enables you to pass data and call functions executed by MATLAB..

Can you run MATLAB scripts from Python?

There are two approaches for calling MATLAB code from Python. The first is to use the MATLAB Engine API for Python, which requires a MATLAB install. The second is to use MATLAB Compiler SDK to compile a Python package that does not require users to have a MATLAB install. Let's first see our MATLAB code.

Can Python and MATLAB work together?

MATLAB® provides a flexible, two-way integration with many programming languages, including Python. This allows different teams to work together and use MATLAB algorithms within production software and IT systems.

How does MATLAB integrate with Python?

To integrate a MATLAB® Compiler SDK™ Python® Package:.
In consultation with the MATLAB programmer, collect the MATLAB function signatures that comprise the services in the application..
Install and import the compiled Python package. ... .
Write the Python code to initialize MATLAB Runtime and load the MATLAB code..