Main Content

Access Python Modules from MATLAB - Getting Started

You can access all standard Python® library content from MATLAB®. To call Python functionality directly from MATLAB, add the py. prefix to the name of the Python function that you want to call. You do not need to import modules.

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. To verify that you have a supported version, type:

pyenv
ans = 

  PythonEnvironment with properties:

          Version: "3.6"
       Executable: "C:\Users\aname\AppData\Local\Programs\Python\Python36\pythonw.exe"
          Library: "C:\Users\aname\AppData\Local\Programs\Python\Python36\python36.dll"
             Home: "C:\Users\aname\AppData\Local\Programs\Python\Python36"
           Status: NotLoaded
    ExecutionMode: InProcess

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 no properties.

    ['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 no properties.

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

To convert the list variable to a MATLAB variable, call cell on the list and char on the elements of the list.

mylist = cellfun(@char,cell(res),'UniformOutput',false)
mylist =

  1×4 cell 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 commands for creating list, tuple, and dict types. The commands on the left are run from the Python interpreter. The commands on the right are MATLAB commands.

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(pyargs(...
'Robert',357,'Mary',229,'Joe',391))


For information about passing keyword arguments, see pyargs.

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 Python Libraries in MATLAB page. For information about searching MATLAB examples, see MATLAB Code Examples.

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

See Also

Related Topics