Skip to main content

A module to call MATLAB from Python

Project description

MATLAB Engine API for Python

The MATLAB® Engine API for Python® provides a package to integrate MATLAB functionality directly with a Python application, creating an interface to call functions from your MATLAB installation from Python code.


Requirements

Required MathWorks Products

  • MATLAB release R2022a

Required 3rd Party Products

  • Python 3.8 or 3.9
    • Supported Python versions by MATLAB release can be found here.

Install

Windows

MATLAB Engine API for Python can be installed directly from the Python Package Index.

$ python -m pip install matlabengine==9.12.10

Linux®

Prior to installation, check the default install location of MATLAB by calling matlabroot in a MATLAB Command Window. By default, Linux installs MATLAB at:

/usr/local/MATLAB/R2022a

When MATLAB is not installed in the default location, the bin/architecture directory within the MATLAB root directory must be added to an environment variable. The path can be added to the environment variable within the shell startup configuration file (for example, .bashrc for bash shell or .tcshrc for tcsh).

# in .bashrc
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:matlabroot/bin/glnxa64
# in .tcshrc
setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:matlabroot/bin/glnxa64

MATLAB Engine API for Python can be installed directly from the Python Package Index.

$ python -m pip install matlabengine==9.12.10

macOS

Prior to installation, check the default install location of MATLAB by calling matlabroot in a MATLAB Command Window. By default, macOS installs MATLAB at:

/Applications/MATLAB_R2022a.app

When MATLAB is not installed in the default location, the bin/architecture directory within the MATLAB root directory must be added to an environment variable. The path can be added to the environment variable within the shell startup configuration file (for example, .bashrc for bash shell or .tcshrc for tcsh).

# in .bashrc
export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:matlabroot/bin/maci64
# in .tcshrc
setenv DYLD_LIBRARY_PATH ${DYLD_LIBRARY_PATH}:matlabroot/bin/maci64

MATLAB Engine API for Python can be installed directly from the Python Package Index.

$ python -m pip install matlabengine==9.12.10

Getting Started

  • Start Python.
  • Import the matlab.engine package into the Python session.
  • Start a new MATLAB process by calling start_matlab. The start_matlab function returns a Python object which enables you to pass data and call functions executed by MATLAB.
>>> import matlab.engine
>>> eng = matlab.engine.start_matlab()
>>> eng.sqrt(4.0)
2.0
  • Call either the exit or quit function to stop the engine. Exiting Python with an engine running stops the engine and its MATLAB processes.
>>> eng.quit()

See Start and Stop MATLAB Engine for Python for advanced startup examples.


Examples

You can call any MATLAB function directly and return the results to Python.

>>> eng.plus(2, 3)
5
>>> eng.isprime(37)
True
>>> eng.gcd(100.0, 80.0, nargout=3)
(20.0, 1.0, -1.0)

See Call MATLAB Functions from Python for more usage examples.


Limitations

Limitations of the MATLAB Engine API for Python can be found here.


Troubleshooting

See Troubleshoot MATLAB Errors in Python for troubleshooting assistance.


License

This software is licensed under the MathWorks XSLA License, which is available in the LICENSE.txt file within this repository.


Support

Technical issues or enhancement requests can be submitted here.


Copyright © 2022 MathWorks, Inc. All rights reserved.

Linux® is the registered trademark of Linus Torvalds in the U.S. and other countries.

Mac OS is a trademark of Apple Inc., registered in the U.S. and other countries.

"Python" and the Python logos are trademarks or registered trademarks of the Python Software Foundation, used by MathWorks with permission from the Foundation.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

matlabengine-9.12.10.tar.gz (16.9 kB view details)

Uploaded Source

File details

Details for the file matlabengine-9.12.10.tar.gz.

File metadata

  • Download URL: matlabengine-9.12.10.tar.gz
  • Upload date:
  • Size: 16.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for matlabengine-9.12.10.tar.gz
Algorithm Hash digest
SHA256 5876697f0a7696025a860f39bf7135861a9408d23dcba1a8420ef77c1fcbdf35
MD5 b2786c0d559f12d4995c0a606e8322b5
BLAKE2b-256 bb5d621c0579dd4b1068b78fde52f134a026465e1181f11f0c0c2cf8f5b23f5d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page