Main Content

pyrunfile

RunPythonscript file fromMATLAB

    Description

    example

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

    Unlike thepyrunfunction, variables created in the Python workspace using thepyrunfilefunction are not persistent. Subsequent calls topyrunfiledo 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)assignslhsvariables. 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 filehello.py.

    Create Python scripthello.pyfrom 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.

    Creategreeting.pyfrom these statements:

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

    将一个字符串传递给脚本和显示输出.

    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 scriptmakeList.pyfrom this statement:

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

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

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

    Call a Python script that takes input arguments.

    Create Python scriptaddac.pyfrom these statements. The script takes input argumentsxandyand returns variablez.

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

    Pass values forxandy. Return the variablezin the MATLAB variableres.

    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 ofcodeto each variable named byoutputsand returns the values inoutvars.

    Example:"L"

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

    Example:x=3

    Output Arguments

    collapse all

    一个或多个MATLAB工作区variable names, returned as valid types fromcode. The number ofoutvarscorresponds to the number ofoutvarsarguments. If you want to access Python data, then you must explicitly return Python objects to MATLAB usingoutvars.

    Version History

    Introduced in R2021b