Main Content

CallingPythonfromMATLAB

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. See. For example:
pyrun(“l =['一', 'new', 'list']") % Call list in Python interpreter

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

Funzioni

espandi tutto

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异常

Argomenti

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 (listortuple) 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.

Risoluzione dei problemi

Determine if Error is Python or MATLAB Error

建议可以确定一个错误起源于Pythonor 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.

Esempi in primo piano