This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

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 traceback and shows the error message only.)

import matlab.engine
eng = matlab.engine.start_matlab()
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.


SyntaxError: Expression Not Valid Target

You can call the MATLAB eval function from Python to create MATLAB variables. (This code sample omits the Python traceback 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 input argument 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)

SyntaxError: Invalid Syntax

Call the MATLAB print function from Python 2.7 to print a plot you create with the MATLAB surf function.

import matlab.engine
eng = matlab.engine.start_matlab()
  File "<stdin>", line 1
SyntaxError: invalid syntax

MATLAB and Python functions can have the same name. When this happens, the engine calls the MATLAB function.

However, the engine cannot directly call a MATLAB function that has a name that also is a reserved word in the Python language. For example, in Python 2.7, print is a reserved word. (In Python 3.x, the previous code runs because print is a built-in function, not a reserved word.)

To avoid this error, call the MATLAB function with eval.


If the MATLAB function is a function that you created, you can rename it so that its name is no longer a Python reserved word. The Python documentation lists reserved words:

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:


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'

Related Topics

Was this topic helpful?