Main Content

Call Python from MATLAB

Directly call Python® functionality from MATLAB®

You can access Python libraries directly from MATLAB by adding the py. prefix to the Python name. See Access Python Modules from MATLAB - Getting Started. For example:

py.list({'This','is a','list'})      % Call built-in function list
py.textwrap.wrap('This is a string') % Call wrap function in module textwrap
You can execute Python statements in the Python interpreter directly from MATLAB using the pyrun or pyrunfile functions. For example:
pyrun("l = ['A','new','list']")  % Call list in Python interpreter
For more information, see Directly Call Python Functionality from MATLAB.

If instead you want to call MATLAB functions from Python applications, see Call MATLAB from Python for more information.

Fonctions

développer tout

pyenvChange default environment of Python interpreter
PythonEnvironmentPython environment information
pyrunRun Python statements from MATLAB
pyrunfileRun Python script file from MATLAB
pyargsCreate keyword arguments for Python function
matlab.exception.PyExceptionCapture error information for Python exception

Rubriques

Use Python Libraries in MATLAB

Run Python Code from MATLAB

Passing Data

Résolution des problèmes

Determine if Error is Python or MATLAB Error

Tips to determine if an error originates in Python or MATLAB code.

Limitations to Python Support

Python features not supported in MATLAB.

Handle Python Exceptions

MATLAB catches exceptions thrown by Python and converts them into a matlab.exception.PyException object.

Troubleshooting Matrix and Numeric Argument Errors

Error might be caused by input array with more than one non-singleton dimension.

Error Converting Elements of list or tuple

How to use string and numeric converters for list and tuple types.