Package: com.mathworks.engine
Java class using MATLAB as a computational engine
The com.mathworks.engine.MatlabEngine
class
uses a MATLAB® process as a computational engine for Java®.
This class provides an interface between the Java language and MATLAB,
enabling you to evaluate MATLAB functions and expressions from Java.
The MatlabEngine
class provides static methods
to start MATLAB and to connect to a shared MATLAB session
synchronously or asynchronously. Only these static methods can instantiate
this class:
Start MATLAB synchronously — startMatlab
Connect to shared MATLAB session synchronously — connectMatlab
Start MATLAB asynchronously — startMatlabAsync
Connect to shared MATLAB session asynchronously — connectMatlabAsync
The engine does not support these MATLAB startup options:
-h
-help
-?
-n
-e
-softwareopengl
-logfile
For information on MATLAB startup options, see Commonly Used Startup Options.
Start MATLAB synchronously. | |
Start MATLAB asynchronously. | |
Find all available shared MATLAB sessions from a local machine synchronously. | |
Find all available shared MATLAB sessions from a local machine asynchronously. | |
Connect to a shared MATLAB session on a local machine synchronously. | |
Connect to a shared MATLAB session on a local machine asynchronously. |
NULL_WRITER | Use a writer that ignores the contents from the MATLAB command window. |
Evaluate a MATLAB function with arguments synchronously. | |
Evaluate a MATLAB function with arguments asynchronously. | |
Evaluate a MATLAB expression as a string synchronously. | |
Evaluate a MATLAB expression as a string asynchronously. | |
Get a variable from the MATLAB base workspace synchronously. | |
Get a variable from the MATLAB base workspace asynchronously. | |
Put a variable into the MATLAB base workspace synchronously. | |
Put a variable into the MATLAB base workspace asynchronously. | |
Disconnect from the current MATLAB session synchronously. | |
Disconnect from the current MATLAB session asynchronously. | |
Force the shutdown of the current MATLAB session synchronously. | |
Force the shutdown of the current MATLAB session asynchronously. | |
Disconnect or terminate the current MATLAB session. |
startMatlab
static MatlabEngine startMatlab(String[] options)
static MatlabEngine startMatlab()
Start MATLAB synchronously.
| Startup options used to start MATLAB engine. You can specify multiple startup
options. The engine supports all MATLAB startup options, except for the options listed
in Unsupported Startup Options. For a list of
options, see the platform-specific command |
Instance of MatlabEngine
| MATLAB fails to start. |
String[] options = {"-noFigureWindows", "-r", "cd H:"}; MatlabEngine eng = MatlabEngine.startMatlab(options);
startMatlabAsync
static Future<MatlabEngine> startMatlabAsync(String[]
options)
static Future<MatlabEngine> startMatlabAsync()
Start MATLAB asynchronously. Once MATLAB has started, then cancel is a no-op.
| Startup options used to start MATLAB engine. You can specify multiple startup
options. The engine supports all MATLAB startup options, except for the options listed
in Unsupported Startup Options. For a list of
options, see the platform-specific command |
Instance of Future<MatlabEngine>
Future<MatlabEngine> future = MatlabEngine.startMatlabAsync();
findMatlab
static String[] findMatlab()
Find all shared MATLAB sessions on the local machine synchronously.
An array of the names of all shared MATLAB sessions on the local machine, or an empty vector if there are no shared MATLAB sessions available on the local machine.
| If there is a failure during the search for MATLAB sessions. |
String[] engines = MatlabEngine.findMatlab();
findMatlabAsync
static Future<String[]> findMatlabAsync()
Find all shared MATLAB sessions on local machine asynchronously.
An instance of Future<String[]>
Future<String[]> future = MatlabEngine.findMatlabAsync();
connectMatlab
static MatlabEngine connectMatlab(String name)
static MatlabEngine connectMatlab()
Connect to a shared MATLAB session on local machine synchronously.
If you specify the name of a shared MATLAB session, but the engine cannot find a session with that name, the engine throws an exception.
If you do not specify a name and there is no shared MATLAB session available, the engine starts a new shared MATLAB session with default options.
If you do not specify a name and there are shared MATLAB sessions available, the engine connects to the first available session.
| Name of the shared MATLAB session. Use findMatlab to get the names of shared MATLAB sessions. |
An instance of MatlabEngine
| MATLAB fails to start or connect. |
MatlabEngine engine = MatlabEngine.connectMatlab();
connectMatlabAsync
static Future<MatlabEngine> connectMatlabAsync(String
name)
static Future<MatlabEngine> connectMatlabAsync
Connect to a shared MATLAB session on local machine asynchronously. The behavior is the same
as that of connectMatlab
except the mechanism is
asynchronous. Once a connection has been made to MATLAB, then cancel is a no-op.
| Name of the shared MATLAB session. |
An instance of Future<MatlabEngine>
Future<MatlabEngine> future = MatlabEngine.connectMatlabAsync();
feval
<T> T feval(int nlhs, String func, Writer output,
Writer error, Object… args)
<T> T feval(int nlhs, String func, Object…
args)
<T> T feval(String func, Writer output, Writer
error, Object… args)
<T> T feval(String func, Object… args)
Evaluate MATLAB functions with input arguments synchronously.
| Name of the MATLAB function or script to evaluate. |
| Number of expected outputs. Default is 1. If If If |
| Stream used to store the standard output from the MATLAB function.
If you do not specify a writer, the output is written to the command
window or terminal. Use |
| Stream used to store the standard error from the MATLAB function.
If you do not specify a writer, the output is written to the command
window or terminal. Use |
| Arguments to pass to the MATLAB function. |
Result of executing the MATLAB function
| Evaluation of a MATLAB function was canceled. |
| Evaluation of a MATLAB function was interrupted. |
| The MATLAB session is not available. |
| There is a MATLAB runtime error in the function. |
| There is an unsupported data type. |
| There is a syntax error in the MATLAB function. |
double result = engine.feval("sqrt", 4);
fevalAsync
<T> Future<T> fevalAsync(int nlhs, String
func, Writer output, Writer error, Object… args)
<T> Future<T> fevalAsync(int nlhs, String
func, Object… args)
<T> Future<T> fevalAsync(String func, Writer
output, Writer error, Object… args)
<T> Future<T> fevalAsync(String func, Object…
args)
Evaluate MATLAB functions with input arguments asynchronously.
| Name of the MATLAB function or script to evaluate. |
| Number of expected outputs. Default is If If If |
| Stream used to store the standard output from the MATLAB function.
If you do not specify a writer, the output is written to the command
window or terminal. Use |
| Stream used to store the standard error from the MATLAB function.
If you do not specify a writer, the output is written to the command
window or terminal. Use |
| Arguments to pass to the MATLAB function. |
An instance of Future<T>
| The MATLAB session is not available. |
Future<Double> future = engine.fevalAsync("sqrt", 4);
eval
void eval(String command, Writer output, Writer error)
void eval(String command)
Evaluate a MATLAB statement as a string synchronously.
| MATLAB statement to evaluate. |
| Stream used to store the standard output from the MATLAB statement.
If you do not specify a writer, the output is written to the command
window or terminal. Use |
| Stream used to store the standard error from the MATLAB statement.
If you do not specify a writer, the output is written to the command
window or terminal. Use |
| Evaluation of a MATLAB function was canceled. |
| Evaluation of a MATLAB function was interrupted. |
| The MATLAB session is not available. |
| There is an error in the MATLAB statement during runtime. |
| There is a syntax error in the MATLAB statement. |
engine.eval("result = sqrt(4)");
evalAsync
Future<Void> evalAsync(String command, Writer
output, Writer error)
Future<Void> evalAsync(String command)
Evaluate a MATLAB statement as a string asynchronously.
| MATLAB statement to evaluate. |
| Stream used to store the standard output from the MATLAB statement.
If you do not specify a writer, the output is written to the command
window or terminal. Use |
| Stream used to store the standard error from the MATLAB statement.
If you do not specify a writer, the output is written to the command
window or terminal. Use |
An instance of Future<Void>
| The MATLAB session is not available. |
Future<Void> future = engine.evalAsync("sqrt(4)");
getVariable
<T> T getVariable(String varName)
Get a variable from the MATLAB base workspace.
| Name of a variable in the MATLAB base workspace. |
Variable passed from the MATLAB base workspace
| Evaluation of this function is canceled. |
| Evaluation of this function is interrupted. |
| The MATLAB session is not available. |
double myVar = engine.getVariable("myVar");
getVariableAsync
<T> Future<T> getVariableAsync(String varName)
Get a variable from the MATLAB base workspace asynchronously.
| Name of a variable in MATLAB base workspace. |
An instance of Future<T>
| The MATLAB session is not available. |
Future<Double> future = engine.getVariableAsync("myVar");
putVariable
void putVariable(String varName, T varData)
Put a variable into the MATLAB base workspace.
| Name of a variable to create in the MATLAB base workspace. |
| Value of the variable to create in the MATLAB base workspace. |
| Evaluation of this function is canceled. |
| Evaluation of this function is interrupted. |
| The MATLAB session is not available. |
engine.putVariable("myVar", 100);
putVariableAsync
Future<Void> putVariableAsync(String varName,
T varData)
Put a variable into the MATLAB base workspace asynchronously.
| Name of a variable to create in the MATLAB base workspace. |
| Value of the variable to create in the MATLAB base workspace. |
An instance of Future<Void>
| The MATLAB session is not available. |
Future<Void> future = engine.putVariableAsync("myVar", 100);
disconnect
void disconnect()
Disconnect from the current MATLAB session.
| The current MATLAB session cannot be disconnected. |
engine.disconnect();
disconnectAsync
Future<Void> disconnectAsync()
Disconnect from the current MATLAB session.
Future<Void> future = engine.disconnectAsync();
quit
void quit()
Force the shutdown of the current MATLAB session.
| The current MATLAB session cannot be shut down. |
engine.quit();
quitAsync
Future<Void> quitAsync()
Force the shutdown of the current MATLAB session asynchronously without waiting for termination.
An instance of Future<Void>
Future<Void> future = engine.quitAsync();
close
void close()
MatlabEngine
provides the close()
method
to implement the java.lang.AutoCloseable
interface
for MatlabEngine
objects. This close()
method
enables you to use a try
-with-resources statement
to automatically disconnect or terminate the MATLAB session at
the end of the statement.
The MatlabEngine
close()
method
disconnects or terminates the current MATLAB session, depending
on the context.
If a Java process starts the MATLAB session
as a default non-shared session, close()
terminates MATLAB.
If the MATLAB session is a shared session, close()
disconnects MATLAB from
this Java process. MATLAB terminates when there are no other
connections.
To force the shutdown or disconnection of the current MATLAB session,
explicitly call MatlabEngine.quit()
, MatlabEngine.disconnect()
,
or their asynchronous counterparts.
engine.close();
import com.mathworks.engine.*; public class StartMatlab { public static void main(String[] args) throws Exception { MatlabEngine eng = MatlabEngine.startMatlab("-desktop"); ... eng.close(); } }
This example shows how to evaluate a MATLAB function asynchronously. The workflow is:
Open a MATLAB session.
Invoke the MATLAB sqrt
function
with arguments asynchronously.
Get the result of the MATLAB function.
Close the MATLAB engine.
import com.mathworks.engine.MatlabEngine Future<MatlabEngine> engFuture = MatlabEngine.startMatlabAsync(); MatlabEngine engine = engFuture.get(); double myVar = 4; Future<Double> future = engine.fevalAsync("sqrt", myVar); double result = future.get(); System.out.println(result);