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.
Functions
expand all
Environment
pyenv | Change default environment of Python interpreter |
PythonEnvironment | Python environment information |
Run Python Code
pyrun | Run Python statements from MATLAB |
pyrunfile | Run Python script file from MATLAB |
Keyword Arguments
pyargs | Create keyword arguments for Python function |
Exception Handling
matlab.exception.PyException | Capture error information for Python exception |
Topics
Use Python Libraries in MATLAB
- Access Python Modules from MATLAB - Getting Started
How to create and use a Python object in MATLAB. - Configure Your System to Use Python
How to verify you have installed a supported version of Python. - Call User-Defined Python Module
Create a Python module used by examples in this documentation. - Understand Python Function Arguments
Python method syntax which might be unfamiliar to MATLAB users. - Advanced Topics
Code pattern differences you should be aware of. - Out-of-Process Execution of Python Functionality
Execute Python scripts in processes that are separate from the MATLAB process. - Reload Out-of-Process Python Interpreter
Reload out-of-process Python interpreter without restarting MATLAB.
Run Python Code from MATLAB
- Directly Call Python Functionality from MATLAB
Ways to call Python from MATLAB.
Passing Data
- MATLAB to Python Data Type Mapping
How MATLAB converts MATLAB data into compatible Python data types. - Access Elements in Python Container Types
A Python container is typically a sequence type (list or tuple) or a mapping type (dict). - Pass Python Function to Python map Function
This example shows how to display the length of each word in a list.
Troubleshooting
Determine if Error is Python or MATLAB Error
Tips to determine if an error originates in Python or MATLAB code.
- Unable to resolve the name py.myfunc
- Debug Python Code Using MATLAB Interface
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.
Featured Examples
Call Python Function in MATLAB to Wrap Paragraph Text
Use Python language functions and modules within MATLAB. The example calls a text-formatting module from the Python standard library.
Open Live Script
Use Python Numeric Variables in MATLAB
Use Python numeric variables with MATLAB.
Open Live Script
Use Python str Variables in MATLAB
Use Python str variables with MATLAB.
Open Live Script
Use Python list Variables in MATLAB
Use Python list variables with MATLAB.
Open Live Script
Use Python tuple Variables in MATLAB
Use Python tuple variables with MATLAB.
Open Live Script
Use Python dict Variables in MATLAB
Use Python dict variables with MATLAB.
Open Live Script