Documentation

妈TLAB API forPython

Write Python®programs that work with MATLAB®

The MATLAB Engine API for Python provides a package for Python to call MATLAB as a computational engine. The engine supports the reference implementation (CPython) for Python versions 2.7, 3.5, and 3.6.

Functions

expand all

matlab.engine.start_matlab Start MATLAB Engine for Python
matlab.engine.find_matlab Find shared MATLAB sessions to connect to MATLAB Engine for Python
matlab.engine.connect_matlab Connect shared MATLAB session to MATLAB Engine for Python
matlab.engine.shareEngine Convert running MATLAB session to shared session
matlab.engine.engineName Return name of shared MATLAB session
matlab.engine.isEngineShared Determine if MATLAB session is shared

Classes

expand all

matlab.engine.MatlabEngine Python object using MATLAB as computational engine within Python session
matlab.engine.FutureResult Results of asynchronous call to MATLAB function stored in Python object

Examples and How To

Install MATLAB Engine API for Python

To start the MATLAB engine within a Python session, you first must install the engine API as a Python package.

Install MATLAB Engine API for Python in Nondefault Locations

By default, the installer builds the engine API for Python in thematlabroot\extern\engines\pythonfolder. The installer installs the engine in the default Python folder.

Start and Stop MATLAB Engine for Python

Options for starting the MATLAB Engine for Python.

Connect Python to Running MATLAB Session

How to connect the MATLAB Engine for Python to a shared MATLAB session that is already running on your local machine.

Call MATLAB Functions from Python

How to return an output argument from a MATLAB function. How to read multiple outputs from a function. What to do when the MATLAB function does not return an output argument.

Call MATLAB Functions Asynchronously from Python

This example shows how to call the MATLABsqrtfunction asynchronously from Python and retrieve the square root later.

Call User Script and Function from Python

This example shows how to call a MATLAB script to compute the area of a triangle from Python.

Redirect Standard Output and Error to Python

This example shows how to redirect standard output and standard error from a MATLAB function to PythonStringIOobjects.

Use MATLAB Engine Workspace in Python

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

Use MATLAB Handle Objects in Python

This example shows how to create an object from a MATLAB handle class and call its methods in Python.

Use MATLAB Arrays in Python

This example shows how to create a MATLAB array in Python and pass it as the input argument to the MATLABsqrtfunction.

Sort and Plot MATLAB Data from Python

This example shows how to sort data about patients into lists of smokers and nonsmokers in Python and plot blood pressure readings for the patients with MATLAB.

Get Help for MATLAB Functions from Python

From Python, you can access supporting documentation for all MATLAB functions.

Concepts

Get Started with MATLAB Engine API for Python

The MATLAB Engine API for Python provides a Python package namedmatlabthat enables you to call MATLAB functions from Python.

System Requirements for MATLAB Engine API for Python

What you need to write and build MATLAB engine applications.

Pass Data to MATLAB from Python

When you pass Python data as input arguments to MATLAB functions, the MATLAB Engine for Python converts the data into equivalent MATLAB data types.

Handle Data Returned from MATLAB to Python

When MATLAB functions return output arguments, the MATLAB Engine API for Python converts the data into equivalent Python data types.

妈TLAB Arrays as Python Variables

ThematlabPython package provides array classes to represent arrays of MATLAB numeric types as Python variables so that MATLAB arrays can be passed between Python and MATLAB.

Default Numeric Types in MATLAB and Python

妈TLAB stores all numeric values as double-precision floating point numbers by default.

Troubleshooting

Limitations to MATLAB Engine API for Python

The engine cannot start or connect to MATLAB on a remote machine.

Troubleshoot MATLAB Errors in Python

When a MATLAB function raises an error, the MATLAB Engine for Python stops the function and catches the exception raised by MATLAB.

Related Information

Was this topic helpful?