Main Content

CallingMATLABfromJava

Write Java®programs that work with MATLAB®

Call MATLAB functions and evaluate MATLAB statements from Java programs synchronously or asynchronously. Java programs can pass data to and get data from the MATLAB base workspace. The engine supports Java applications built with JDK™ 7 or 8.

To call Java functions from MATLAB, seeJava Libraries in MATLAB.

Functions

expand all

matlab.engine.shareEngine Convert runningMATLABsession to shared session
matlab.engine.engineName Return name of sharedMATLABsession
matlab.engine.isEngineShared Determine ifMATLABsession is shared

Classes

expand all

com.mathworks.engine.MatlabEngine Javaclass usingMATLABas a computational engine
com.mathworks.matlab.types.Complex Javaclass to pass complex data to and fromMATLAB
com.mathworks.matlab.types.Struct Javaclass to passMATLABstructto and fromMATLAB
com.mathworks.matlab.types.CellStr Javaclass to representMATLABcell array ofcharvectors
com.mathworks.matlab.types.HandleObject Javaclass to representMATLABhandle objects

Topics

开始

MATLAB Engine API for Java

This API enables Java programs to use MATLAB as a computational engine.

Build Java Engine Programs

Set environment variables, Java classpath, and compile and run your Java code.

Java示例源代码

These examples show how to use the MATLAB Engine API for Java.

JavaEngine API Overview

Java Engine API Summary

Here is a summary of the classes and methods available to use the MATLAB Engine API for Java.

Java Data Type Conversions

MATLAB引擎API提供了映射通过农协va data types to MATLAB and pass MATLAB data types to Java, including Java classes to accommodate MATLAB handle objects, cellstrs, structures, and complex arrays.

UsingMATLABEngine API forJava

Start and Close MATLAB Session from Java

Start the Java engine session synchronously or asynchronously and you can pass startup options to MATLAB.

Connect Java to Running MATLAB Session

Connect the Java engine to a running MATLAB session that has been started as or converted to a shared session.

Execute MATLAB Functions from Java

Pass Java variables to MATLAB function calls and return values to Java.

Evaluate MATLAB Statements from Java

Evaluate MATLAB expressions from Java and write variables into the MATLAB base workspace.

Pass Variables from Java to MATLAB

Pass variables from Java to MATLAB as function arguments or by placing those variables in the MATLAB base workspace.

Pass Variables from MATLAB to Java

Get variables from the MATLAB workspace and map them to the appropriate Java type.

Redirect MATLAB Command Window Output to Java

Redirect standard output from the MATLAB command window to Java.

SpecializedMATLABTypes forJava

Using Complex Variables in Java

Define complex numbers in Java using theComplexclass.

Using MATLAB Structures in Java

Define MATLAB structure data types in Java using theStructclass.

Pass Java CellStr to MATLAB

Define a MATLAB cell array of character vectors using the JavaCellStrclass.

Using MATLAB Handle Objects in Java

Return MATLAB handle objects to Java using the JavaHandleObjectclass.

UsingMATLABFeatures fromJava

Run Simulink Simulation from Java

You can run Simulink®simulations and return results to Java.

Troubleshooting

MATLAB Engine API Exceptions

The MATLAB Engine API for Java defines specific exception classes for MATLAB.

Pass Java Array Arguments to MATLAB

MATLAB and Java display multidimensional arrays differently, but indexed references to array elements produce the same results.

Incorrect Number of Output Arguments

If you are calling a MATLAB function with no or more than one output argument, you must provide that number in calls to the enginefevalmethod.

Incorrect Java Data Types

In certain cases, you must convert to specific MATLAB types to support the requirements of the MATLAB function you are calling.

Related Information