Skip to content

pyrun

matlab_compat · scripts/matlab_compat/pyrun.m

: pyrun (code)
: outvars = pyrun (code, outputs)
: outvars = pyrun (code, outputs, pyName, pyValue, …)

Run Python statements from Nexus (MATLAB R2026a pyrun parity).

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

outvars = pyrun (code, 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:Pyrun:InvalidCodeType, MATLAB:Pyrun:InvalidOutputList, MATLAB:Python:PyException, MATLAB:maxlhs.