Troubleshoot MATLAB Errors in Python
MATLAB Errors in Python
When a MATLAB® function raises an error, the MATLAB Engine for Python® stops the function and catches the exception raised by MATLAB. The engine copies the error message to a new Python exception. The engine raises the Python exception.
If the Python interpreter catches the exception, the interpreter displays the error
message that came from MATLAB. You also can handle exceptions raised by the engine in your
Python code. See the matlab.engine.MatlabEngine
and
matlab.engine.FutureResult
reference pages for the types of
exceptions that the engine can raise.
MatlabExecutionError: Undefined Function
Call the MATLAB
sqrt
function on an integer from Python. (This code sample omits the Python trace back and shows the error message only.)
import matlab.engine
eng = matlab.engine.start_matlab()
print(eng.sqrt(4))
matlab.engine.MatlabExecutionError: Undefined function 'sqrt' for input arguments of type 'int64'.
MATLAB defines a sqrt
function, but expects the input
argument to be of data type double
, not an integer. However, the
input argument is 4, and before it is passed to MATLAB, Python interprets 4 as an integer. The engine converts the Python integer to an int64
MATLAB data type.
MATLAB and Python define different default types for numbers. If you type x =
4
at the MATLAB command line, x
is a MATLAB
double
. If you type x = 4
at the Python command line, x
is a Python
int
.
To avoid this error, specify input arguments that are of Python data type float
. The engine converts this type to
MATLAB
double
.
print(eng.sqrt(4.0))
2.0
SyntaxError: Expression Not Valid Target
You can call the MATLAB
eval
function from Python to create MATLAB variables. (This code sample omits the Python trace back and shows the error message only.)
import matlab.engine
eng = matlab.engine.start_matlab()
eng.eval("x = 4;")
SyntaxError: Error: The expression to the left of the equals sign is not a valid target for an assignment.
When the engine calls eval
, it passes a statement to
MATLAB for execution. When you do not specify the nargout
input argument, the engine expects one output argument. However, this MATLAB statement returns no output arguments.
To avoid this error, specify nargout
as 0 whenever the
MATLAB function you call returns no output arguments.
eng.eval("x = 4;",nargout=0)
Cannot Find MATLAB Session in Python
If you override the operating system TEMP
or
TMP
environment variables in MATLAB, Python might not be able to connect to the MATLAB Engine for Python. For example, if you type the following at the Python prompt:
matlab.engine.find_matlab()
Python displays ()
.
MATLAB Engine for Python uses the temp folder to record information for shared MATLAB sessions. To work around this issue, make the following changes to the
environment variables in Python. temp_folder
is the path to the folder which you
set in MATLAB.
os.environ['TMP'] = r'temp_folder'
os.environ['TEMP'] = r'temp_folder'
eng=matlab.engine.find_matlab()