Call Back intoMATLABfromJava
A Java®application designed for use inside of a MATLAB®program can call back into the MATLAB code using thegetCurrentMatlabmethod in thecom.mathworks.engine.MatlabEngine
API. For information about using this API, seeMATLAB Engine API for Java.
For example, the code in this Java classExampleClass
creates a methodfevalExample
to call the MATLABsqrt
函数。This method is part of a larger application which might read data from a device and then apply the MATLAB function on the data. In thefevalExample
method, connect to MATLAB usinggetCurrentMatlab
. The application manages the data between the device and the MATLAB calculation. MATLAB users call thefevalExample
function to bring the data into MATLAB for further action.
import com.mathworks.engine.*; public class ExampleClass { private MatlabEngine engine; public double fevalExample() throws Exception { engine = MatlabEngine.getCurrentMatlab(); double sqrtOut = engine.feval("sqrt", 4.0); engine.close(); return sqrtOut; } }
To callfevalExample
from MATLAB, addExampleClass
to the Java class path. This example assumes that the file is in your current folder. Create MATLAB objectjavaTest
and call itsfevalExample
函数。Theresult
is the value returned bysqrt
.
javaaddpath(pwd) javaTest = ExampleClass; result = javaTest.fevalExample()
result = 2.0
Note
Programs using thegetCurrentMatlab
method are supported on the MATLAB thread only. If you call this functionality from an engine application, MATLAB displays an error.
See Also
com.mathworks.engine.MatlabEngine
|getCurrentMatlab