Invoke a Packaged MATLAB Function
Invoke a compiled MATLAB® function by using the Python® object returned from initialize()
.
result1,...resultN = <my_client>.function_name(<in_args>, nargout=<nargs>,
stdout=<out_stream>,
stderr=<err_stream>)
my_client
— Name of object returned frominitialize()
function_name
— Name of the function to invokein_args
— Comma-separated list of input argumentsnargs
— Number of expected results. The default value is1
.out_stream
— PythonStringIO
object receiving the console output. The default is to direct output to the console.err_stream
— PythonStringIO
object receiving the error output. The default is to direct output to the console.
Each variable on the left side of the function call is populated with a single return value.
Note
If you provide fewer than nargs
variables on the
left side of the function call, the last listed variable contains a list of the
remaining results. For example
result1, result2 = myMagic.triple(5,nargout=3)
Invoke MATLAB Function with Single Output
To invoke the MATLAB function result = mutate(m1, m2, m3)
from the
package mutations
, you use this code.
import mutations
import matlab
myMutator = mutations.initialize()
m1 = matlab.double([1,2,3])
m2 = matlab.double([10,20,30])
m3 = matlab.double([100,200,300])
result = myMutator.mutate(m1,m2,m3)
Invoke MATLAB Function with Zero Outputs
To invoke the MATLAB function mutate(m1,m2,m3)
from the package
mutations
, you use this code.
import mutations
import matlab
myMutator = mutations.initialize()
m1 = matlab.double([1,2,3])
m2 = matlab.double([10,20,30])
m3 = matlab.double([100,200,300])
myMutator.mutate(m1,m2,m3,nargout=0)
Receive Multiple Results as Individual Variables
To invoke the MATLAB function c1,c2 = copy(o1,o2)
from the package
copier
, use this code.
>>> import copier
>>> import matlab
>>> myCopier = copier.initialize()
>>> c1,c2 = myCopier.copy("blue",10,nargout=2)
>>> print(c1)
"blue"
>>> print(c2)
10
Receive Multiple Results as Single Object
To invoke the MATLAB function copies = copy(o1,o2)
from the package
copier
, use this code.
>>> import copier
>>> import matlab
>>> myCopier = copier.initialize()
>>> copies = myCopier.copy("blue",10,nargout=2)
>>> print(copies)
["blue",10]
Invoke a MATLAB Function in the Background
To invoke a MATLAB function sumwrap
from the package
sumwrap
asynchronously, use this code.
>>> import sumwrap
>>> import matlab
>>> sw = sumwrap.initialize()
>>> a = matlab.double([[1, 2],[3, 4]])
>>> future = sw.sumwrap(a, 1, background=True)
>>> future.result()
matlab.double([[4.0,6.0]])
Note
If a packaged MATLAB function that calls Python is invoked, and Python is running in
InProcess
mode, the following error
occurs.
When using MATLAB Compiler SDK, Python libraries can be called from MATLAB only after pyenv is called with the ''ExecutionMode'' argument set to ''OutOfProcess''.
OutOfProcess
mode, the function runs
as intended.