Skip to content

pyrunfile

matlab_compat · scripts/matlab_compat/pyrunfile.m

: pyrunfile (file)
: pyrunfile (file_input)
: outvars = pyrunfile (file, outputs)
: outvars = pyrunfile (file, outputs, pyName, pyValue, …)

Run a Python script file from Nexus (MATLAB R2026a pyrunfile parity).

pyrunfile (file) executes the Python statements in file. Variables created by pyrunfile are not persistent across calls.

pyrunfile ("script.py 'arg'") passes command-line arguments that the script reads via sys.argv (same quoting rules as MATLAB).

outvars = pyrunfile (file, outputs) returns named Python variables. outputs is a character vector, string array, or cellstr of names. When nargout exceeds the number of names, the error id is MATLAB:maxlhs.

Name/value inputs use alternating pyName, pyValue pairs after outputs (MATLAB also accepts Name=Value; Nexus call sites should use char/value pairs because name=value is stripped to the value alone).

Output conversion matches MATLAB R2026a for the documented cases: Python list -> py.list, dict -> py.dict, float -> double, bool -> logical, str -> py.str (char-backed stub), int -> py.int (numeric stub), None -> py.NoneType, numpy.ndarray -> native numeric array (dtype preserved; 1-D becomes a row).

Error ids match MATLAB: MATLAB:minrhs, MATLAB:Pyrunfile:EmptyFilename, MATLAB:Pyrunfile:InvalidFilenameType, MATLAB:Pyrunfile:FileNotFound, MATLAB:Pyrun:InvalidOutputList, MATLAB:Python:PyException, MATLAB:maxlhs.