Main Content

Redirect Standard Output and Error to Python

This example shows how to redirect standard output and standard error from a MATLAB® function to Python® StringIO objects.

In Python 2.7, use the StringIO module to create StringIO objects. To capture a warning message from dec2hex, specify stdout and stderr.

import matlab.engine
eng = matlab.engine.start_matlab()
import StringIO
out = StringIO.StringIO()
err = StringIO.StringIO()
ret = eng.dec2hex(2**60,stdout=out,stderr=err)
print(out.getvalue())
Warning: At least one of the input numbers is larger than the largest integer-valued 
floating-point number (2^52). Results may be unpredictable.

In Python 3.x, use the io module to create StringIO objects.

import matlab.engine
eng = matlab.engine.start_matlab()
import io
out = io.StringIO()
err = io.StringIO()
ret = eng.dec2base(2**60,16,stdout=out,stderr=err)

dec2base raises an exception when an input argument is greater than 2^52. Display the error message captured in err.

print(err.getvalue())
Error using dec2base (line 22)
First argument must be an array of integers, 0 <= D <= 2^52.

See Also

|

Related Topics