|
| 1 | +# MATLAB Engine API for Python |
| 2 | + |
| 3 | +The MATLAB® Engine API for Python® provides a package to integrate MATLAB functionality directly with a Python application, creating an interface to call your MATLAB functions from Python code. |
| 4 | + |
| 5 | +--- |
| 6 | +## Requirements |
| 7 | +### Required MathWorks Products |
| 8 | +<!-- MUST_BE_UPDATED_EACH_RELEASE (Search repo for this string) --> |
| 9 | +* MATLAB release R2022b |
| 10 | + |
| 11 | +### Required 3rd Party Products |
| 12 | +<!-- MUST_BE_UPDATED_EACH_RELEASE (Search repo for this string) --> |
| 13 | +* Python 3.8, 3.9, or 3.10 |
| 14 | + * Supported Python versions by MATLAB release can be found [here](https://www.mathworks.com/content/dam/mathworks/mathworks-dot-com/support/sysreq/files/python-compatibility.pdf). |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## Install |
| 19 | + |
| 20 | +### Windows |
| 21 | +MATLAB Engine for Python can be installed directly from the Python Package Index. |
| 22 | +<!-- MUST_BE_UPDATED_EACH_RELEASE (Search repo for this string) --> |
| 23 | +```bash |
| 24 | +$ python -m pip install matlabengine==9.13 |
| 25 | +``` |
| 26 | + |
| 27 | + |
| 28 | + |
| 29 | +### Linux® |
| 30 | +Prior to installation, check the default install location of MATLAB by calling ```matlabroot``` in a MATLAB Command Window. By default, Linux installs MATLAB at:<br> |
| 31 | +<!-- MUST_BE_UPDATED_EACH_RELEASE (Search repo for this string) --> |
| 32 | +```/usr/local/MATLAB/R2022b``` |
| 33 | + |
| 34 | +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). |
| 35 | + |
| 36 | +```bash |
| 37 | +# in .bashrc |
| 38 | +export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:matlabroot/bin/glnxa64 |
| 39 | +``` |
| 40 | + |
| 41 | +```bash |
| 42 | +# in .tcshrc |
| 43 | +setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:matlabroot/bin/glnxa64 |
| 44 | +``` |
| 45 | + |
| 46 | +MATLAB Engine for Python can be installed directly from the Python Package Index. |
| 47 | +<!-- MUST_BE_UPDATED_EACH_RELEASE (Search repo for this string) --> |
| 48 | +```bash |
| 49 | +$ python -m pip install matlabengine==9.13 |
| 50 | +``` |
| 51 | + |
| 52 | +### macOS |
| 53 | +Prior to installation, check the default install location of MATLAB by calling ```matlabroot``` in a MATLAB Command Window. By default, macOS installs MATLAB at:<br> |
| 54 | + |
| 55 | +<!-- MUST_BE_UPDATED_EACH_RELEASE (Search repo for this string) --> |
| 56 | +```/Applications/MATLAB_R2022b.app``` |
| 57 | + |
| 58 | +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). |
| 59 | + |
| 60 | +```bash |
| 61 | +# in .bashrc |
| 62 | +export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:matlabroot/bin/maci64 |
| 63 | +``` |
| 64 | + |
| 65 | +```bash |
| 66 | +# in .tcshrc |
| 67 | +setenv DYLD_LIBRARY_PATH ${DYLD_LIBRARY_PATH}:matlabroot/bin/maci64 |
| 68 | +``` |
| 69 | + |
| 70 | +MATLAB Engine for Python can be installed directly from the Python Package Index. |
| 71 | +<!-- MUST_BE_UPDATED_EACH_RELEASE (Search repo for this string) --> |
| 72 | +```bash |
| 73 | +$ python -m pip install matlabengine==9.13 |
| 74 | +``` |
| 75 | + |
| 76 | +--- |
| 77 | + |
| 78 | +## Getting Started |
| 79 | +* Start Python. |
| 80 | +* Import the ```matlab.engine``` package into the Python session. |
| 81 | +* 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. |
| 82 | + |
| 83 | +```python |
| 84 | +>>> import matlab.engine |
| 85 | +>>> eng = matlab.engine.start_matlab() |
| 86 | +>>> eng.sqrt(4.0) |
| 87 | +2.0 |
| 88 | +``` |
| 89 | + |
| 90 | +* Call either the ```exit``` or ```quit``` function to stop the engine. Exiting Python with an engine running stops the engine and its MATLAB processes. |
| 91 | + |
| 92 | +```python |
| 93 | +>>> eng.quit() |
| 94 | +``` |
| 95 | + |
| 96 | +See [Start and Stop MATLAB Engine for Python](https://www.mathworks.com/help/matlab/matlab_external/start-the-matlab-engine-for-python.html) for advanced startup examples. |
| 97 | + |
| 98 | +--- |
| 99 | + |
| 100 | +## Examples |
| 101 | +You can call any MATLAB function directly and return the results to Python. |
| 102 | +```python |
| 103 | +>>> eng.plus(2, 3) |
| 104 | +5 |
| 105 | +>>> eng.isprime(37) |
| 106 | +True |
| 107 | +>>> eng.gcd(100.0, 80.0, nargout=3) |
| 108 | +(20.0, 1.0, -1.0) |
| 109 | +``` |
| 110 | +See [Call MATLAB Functions from Python](https://www.mathworks.com/help/matlab/matlab_external/call-matlab-functions-from-python.html) for more usage examples. |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +## Limitations |
| 115 | +Limitations of the MATLAB Engine API for Python can be found [here](https://www.mathworks.com/help/matlab/matlab_external/limitations-to-the-matlab-engine-for-python.html). |
| 116 | + |
| 117 | +--- |
| 118 | + |
| 119 | +## Troubleshooting |
| 120 | +See [Troubleshoot MATLAB Errors in Python](https://www.mathworks.com/help/matlab/matlab_external/troubleshoot-matlab-errors-in-python.html) for troubleshooting assistance. |
| 121 | + |
| 122 | +--- |
| 123 | + |
| 124 | +## License |
| 125 | +This software is licensed under the MathWorks XLSA License, which is available in the LICENSE.txt file within this repository. |
| 126 | + |
| 127 | +--- |
| 128 | + |
| 129 | +## Support |
| 130 | +Technical issues or enhancement requests can be submitted [here](https://github.com/mathworks/matlab-engine-for-python/issues). |
| 131 | + |
| 132 | +--- |
| 133 | + |
| 134 | +Copyright © 2022 MathWorks, Inc. All rights reserved. |
| 135 | + |
| 136 | +Linux® is the registered trademark of Linus Torvalds in the U.S. and other countries. |
| 137 | + |
| 138 | +Mac OS is a trademark of Apple Inc., registered in the U.S. and other countries. |
| 139 | + |
| 140 | +"Python" and the Python logos are trademarks or registered trademarks of the Python Software Foundation, used by MathWorks with permission from the Foundation. |
0 commit comments