Main Content

CallPythonfromMATLAB

Directly call Python®functionality from MATLAB®

You can access Python libraries directly from MATLAB by adding thepy.prefix to the Python name. SeeAccess 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 thepyrunorpyrunfilefunctions. For example:
pyrun("l = ['A','new','list']") % Call list in Python interpreter
For more information, seeDirectly Call Python Functionality from MATLAB.

If instead you want to call MATLAB functions from Python applications, seeCall MATLAB from Pythonfor more information.

Functions

expand all

pyenv Change default environment ofPythoninterpreter
PythonEnvironment Python environment information
pyrun RunPythonstatements fromMATLAB
pyrunfile RunPythonscript file fromMATLAB
pyargs Create keyword arguments forPythonfunction
matlab.exception.PyException Capture error information forPython异常

Topics

Use Python Libraries in MATLAB

Run Python Code from MATLAB

Passing Data

Troubleshooting

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.

处理Python异常

MATLAB catches exceptions thrown by Python and converts them into amatlab.exception.PyExceptionobject.

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 forlistandtupletypes.