Main Content

Access Python Modules from MATLAB - Getting Started

You can access all standard Python® library content from MATLAB®. Likewise, you can use functionality in third-party or user-created modules. To call Python functionality directly from MATLAB, add the py. prefix to the name of the Python function that you want to call.

  • To call content in the Python standard library, add py. in front of the Python function or class name.

    py.list({"This","is a","list"}) % Call built-in function list
  • To call content in available modules, add py. in front of the Python module name followed by the Python function or class name.

    py.textwrap.wrap("This is a string") % Call wrap function in module textwrap

You do not need to import modules in order to use them. However, you may import Python names into your MATLAB function in the same way that you can import content in MATLAB packages. For more information, see Understanding Python and MATLAB import Commands.

MATLAB also provides a way to run Python code in the Python interpreter directly from MATLAB. For more information, see Directly Call Python Functionality from MATLAB.

Learning Objectives

This tutorial explains how to:

  • Check the Python version on your computer.

  • Create a Python object and call a method on it.

  • Display help for Python modules.

  • Create specialized Python list, tuple, and dict (dictionary) types

  • Call a method on a Python object with the same name as a MATLAB function.

  • Call functionality from your own Python module.

  • Find examples.

Verify Python Configuration

To use Python in MATLAB, you must have a supported version of Python installed on your machine. For more information, see Configure Your System to Use Python.

Make sure that the Python path is included in your system path environment variable. To verify that you have a supported version of Python, type:

pyenv
ans = 

  PythonEnvironment with properties:

          Version: "3.11"
       Executable: "C:\Users\aname\AppData\Local\Programs\Python\Python311\pythonw.exe"
          Library: "C:\Users\aname\AppData\Local\Programs\Python\Python311\python311.dll"
             Home: "C:\Users\aname\AppData\Local\Programs\Python\Python311"
           Status: NotLoaded
    ExecutionMode: OutOfProcess

If the value of the Version property is empty, then you do not have a supported version available. For more information about installing Python, see Configure Your System to Use Python.

Access Python Standard Library Modules in MATLAB

MATLAB interacts with the Python interpreter on your machine, giving you access all standard library content. For example, create a Python list data type.

res = py.list({"Name1","Name2","Name3"})
res = 

  Python list with values:

    ['Name1', 'Name2', 'Name3']

MATLAB recognizes Python objects and automatically converts the MATLAB cell array to the appropriate Python type.

You can call Python methods on an object. To display the available methods for list objects, type methods(py.list). For example, update the list res using the Python append function.

res.append("Name4")
res
res = 

  Python list with with values:

    ['Name1', 'Name2', 'Name3', 'Name4']

To convert the list variable to a MATLAB variable, call string.

mylist = string(res)
mylist =

  1×4 string array

    'Name1'    'Name2'    'Name3'    'Name4'

Display Python Documentation in MATLAB

You can display help text for Python functions in MATLAB. For example:

py.help("list.append")
Help on method_descriptor in list:

list.append = append(...)
    L.append(object) -> None -- append object to end

Tab completion when typing py. does not display available Python functionality. For more information, see Help for Python Functions.

Create List, Tuple, and Dictionary Types

This table shows the statements for creating list, tuple, and dict types. The statements on the left are run from the Python interpreter. The statements on the right are MATLAB statements.

Python list[]

MATLAB py.list

>>> ['Robert', 'Mary', 'Joseph']>> py.list({"Robert","Mary","Joseph"})
>>> [[1,2],[3,4]]>> py.list({py.list([1,2]),py.list([3,4])})

Python tuple()

MATLAB py.tuple

>>> ('Robert', 19, 'Biology')>> py.tuple({"Robert",19,"Biology"})

Python dict{}

MATLAB py.dict

>>> {'Robert': 357, 'Joe': 391, 'Mary': 229}>> py.dict(Robert=357,Mary=229,Joe=391)

Precedence Order of Methods and Functions

If a Python class defines a method with the same name as a MATLAB converter method for Python types, MATLAB calls the Python method. This means you cannot call the MATLAB converter method on an object of that class.

For example, if a Python class defines a char method, this statement calls the Python method.

char(obj)

To use the MATLAB char function, type:

char(py.str(obj))

Access Other Python Modules

You can use your own Python code and third-party modules in MATLAB. The content must be on the Python path. Installing a third-party module puts the content on the Python path. If you create your own modules, you are responsible for putting them on the path.

For an example, see Call User-Defined Python Module.

Python Examples

For example code you can open in the MATLAB live editor, look for Featured Examples on the Call Python from MATLAB page. For information about searching MATLAB examples, see MATLAB Code Examples.

For an example using an online dataset, see this bat365 blog post.

See Also

Related Topics