Main Content

pyrunfile

Run Python script file from MATLAB

    Description

    example

    pyrunfile(file) executes the Python® statements in the file.

    Unlike the pyrun function, variables created in the Python workspace using the pyrunfile function are not persistent. Subsequent calls to pyrunfile do not have access to the variables.

    example

    pyrunfile(file input) executes the Python statements with input arguments. Python scripts read command-line arguments as strings.

    example

    outvars = pyrunfile(file,outputs) assigns lhs variables. The functions will also allow users to select MATLAB® workspace variables to be passed as input to the given Python code, and return all or a user-selected subset of Python variables that are processed in the script, back to MATLAB.

    example

    outvars = pyrunfile(file,outputs,pyName=pyValue) executes the code with one or more name-value pair arguments.

    Examples

    collapse all

    Call Python statements in file hello.py.

    Create Python script hello.py from these statements:

    greeting = "hello"
    print(greeting)

    Display the output to the MATLAB command line.

    pyrunfile("hello.py")
    hello

    Create a Python script and pass a string.

    Create greeting.py from these statements:

    import sys
    greeting = sys.argv[1]
    print(greeting)

    Pass a string to the script and display the output.

    pyrunfile("greeting.py 'hello world'")
    hello world

    Run a Python script from MATLAB and return a variable generated by the script to MATLAB.

    Create Python script makeList.py from this statement:

    l = ['A', 'new', 'list']

    Run the script to create the list and return it to MATLAB in variable data.

    data = pyrunfile("makeList.py", "l")
    data = 
      Python list with no properties.
    
        ['A', 'new', 'list']
    

    Call a Python script that takes input arguments.

    Create Python script addac.py from these statements. The script takes input arguments x and y and returns variable z.

    def add(a,c):
        b = a+c
        return b
    
    z = add(x,y)

    Pass values for x and y. Return the variable z in the MATLAB variable res.

    res = pyrunfile("addac.py","z",x=3,y=2)
    res = 5

    Input Arguments

    collapse all

    Python file containing Python expressions or statements, specified as string scalar or character vector.

    Example: "test.py"

    Python file containing Python expressions or statements with command line input arguments, specified as string scalar or character vector.

    Example: "test.py var"

    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: "L"

    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: x=3

    Output Arguments

    collapse all

    One or more MATLAB workspace variable names, returned as valid types from code. The number of outvars corresponds to the number of outvars arguments. If you want to access Python data, then you must explicitly return Python objects to MATLAB using outvars.

    See Also

    Introduced in R2021b