Main Content


Run Python statements from MATLAB

Since R2021b



    pyrun(code) executes the Python® statements in code in the Python interpreter.

    Variables created using the pyrun function are persistent. You can use these variables in subsequent calls to pyrun.


    outvars = pyrun(code,outputs) returns any variable generated by Python to MATLAB®, by specifying the names of the Python variables in the outputs and capturing the returned values in outvars.


    outvars = pyrun(code,outputs,pyName=pyValue) executes the code with assigned input and output variable names using the MATLAB data passed by one or more name-value arguments.


    collapse all

    This example executes these Python statements in the Python interpreter.

    greeting = "hello"

    Call the Python code from MATLAB.

    pyrun(["greeting = 'hello'", "print(greeting)"])

    Variable greeting exists only in the Python namespace. MATLAB displays the results of the print statement at the MATLAB command line.

    This Python code creates a list of the days of the week.

    days = ['Monday','Tuesday','Wednesday','Thursday','Friday']

    Create a Python variable days for the list function. In MATLAB, name the variable mllist.

    mllist = pyrun("days = ['Monday','Tuesday','Wednesday','Thursday','Friday']","days")
    mllist = 
      Python list with no properties.
        ['Tuesday', 'Monday', 'Wednesday', 'Thursday', 'Friday']

    This example executes the statement a = b*c in the Python interpreter with the specified input values.

    pyrun("a = b*c", b = 5, c = 10)

    Variables a, b, and c exist only in the Python namespace. However, these variables are available for further calls to pyrun.

    md = pyrun("d = a+c", "d")
    md = 60

    This example executes b*c in Python and returns the results in a MATLAB variable.

    res = pyrun("a = b*c", "a", b=5, c=10)
    res = 50

    This example assigns a local variable to a Python variable to make it accessible in MATLAB.

    Create a module

    def myFunc():
        print('myFunc executed')
    mvar = 3

    Create variable m to access mvar and assign the value to MATLAB variable out.

    pyrun("import localModule")
    out = pyrun("m = localModule.mvar","m")

    Input Arguments

    collapse all

    One or more Python statements, specified as a string scalar, string array, character vector, character array, cell array of character vectors, or Python code object of a script generated using the Python built-in compile function. Each entry represents a line of Python code.

    To call a single-line statement, pass code as a string scalar or character vector. To call multiline Python statements, pass code as a string array, character array, or cell array of character vectors. MATLAB inserts newline characters between elements of multiline statements.

    Example: pyrun(["a = 3","print(a)"])

    One or more Input argument names and values to pass to the Python code, specified as keyword and value arguments. pyName is the Python name of a variable, and pyValue is the assigned value. You can specify several name and value pair arguments in any order as pyName1=pyValue1,...,pyNameN=pyValueN.

    Example: pyrun("b*c",b=5,c=10) initializes variables b and c for the Python statement b*c.

    One or more Python variable names, specified as a string array. Variables can be local or global. MATLAB assigns the output of code to each variable named by outputs and returns the values in outvars.

    Example: mb = pyrun("b=a+2","b",a=5)

    Output Arguments

    collapse all

    One or more MATLAB workspace variable names, returned as valid Python types from code. Specify the names of the Python variables in the outputs argument. If you want to access Python data, then you must explicitly return Python objects to MATLAB using outvars.

    To specify multiple outputs, use square brackets. For example, [res1,res2] = pyrun("a=b*c",["a","b"],b=5,c=10) returns two outvars, res1 and res2.


    • Python classes defined using pyrun or pyrunfile cannot be modified if you return an instance of the class to MATLAB. If you need to change class definitions, restart the interpreter session:


      Alternatively, restart MATLAB for "InProcess".

      The pyrun and pyrunfile functions do not support classes with local variables that are initialized by other local variables through methods. For such usage, create a module and access it using the py. prefix.

    Version History

    Introduced in R2021b