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.
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
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 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)
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()
eng.eval("surf(peaks)",nargout=0)
eng.print("-djpeg","surf",nargout=0)
File "<stdin>", line 1
eng.print("-djpeg","surf",nargout=0)
^
SyntaxError: invalid syntax
If MATLAB and Python functions have the same name, then 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.
eng.eval("print('-djpeg','surf');",nargout=0)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:
Python 2.7 reserved words (https://docs.python.org/2/reference/lexical_analysis.html#keywords)
Python 3.x reserved
words (https://docs.python.org/3/reference/lexical_analysis.html#keywords)
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()