Use MATLAB Class in Java Application
Overview
This example shows you how to create a Java® application that calls MATLAB® wrapper functions for a MATLAB class.
In this example, you perform the following steps:
Use MATLAB Compiler SDK™ to create a package that uses MATLAB wrapper functions to access a MATLAB class.
Call the MATLAB wrapper functions in a Java application.
Build and run the application.
Procedure
In MATLAB, examine the MATLAB code that you want to package. For this example, create a MATLAB class named
MyMATLABClass.m
using the following code:classdef MyMatlabClass < handle properties (Access = private) x % input variable y % input variable z % result variable end methods function this = MyMatlabClass() this.x = []; this.y = []; end function setInput(this, input) input = input(:); if isnumeric(input) && numel(input) == 2 this.x = input(1); this.y = input(2); end end function result = getResult(this) result = this.z; end function status = compute(this) try this.z = (this.x.^2 + this.y.^2)^0.5; status = true; catch status = false; end end end end
Create four MATLAB wrapper functions for the class:
CreateMyMATLABClass.m
,SetInput.m
,Compute.m
, andGetResult.m
.CreateMyMATLABClass.m.
function instance = CreateMyMATLABClass() instance = MyMATLABClass(); end
Build the Java package with the Library Compiler app or
compiler.build.javaPackage
using the following information:Field Value Library Name MyMATLABClass1
Class Name Class1
Files to Compile CreateMyMATLABClass.m
SetInput.m
Compute.m
GetResult.m
For example, if you are using
compiler.build.javaPackage
, type:buildResults = compiler.build.javaPackage(["CreateMyMATLABClass.m", ... "SetInput.m","Compute.m","GetResult.m"], ... 'PackageName','MyMATLABClass1', ... 'ClassName','Class1');
For more details, see the instructions in Generate Java Package and Build Java Application.
Note
You do not need to manually add the
MyMATLABClass.m
file to the package, as the compiler automatically includes it during dependency analysis.Navigate to the folder that contains the generated
MyMATLABClass1.jar
package. If you used the Library Compiler, the package is in thefor_testing
folder.Write source code for an application that accesses the MATLAB functions. The code for this example is provided below.
Open a command prompt window and navigate to the folder that contains
javadriver.java
andMyMATLABClass.jar
.Compile the
javadriver.java
application usingjavac
.On Windows®, type:
javac -classpath "
matlabroot
\toolbox\javabuilder\jar\javabuilder.jar";.\MyMATLABClass.jar javadriver.javaOn UNIX®, type:
javac -classpath "
matlabroot
/toolbox/javabuilder/jar/javabuilder.jar":./MyMATLABClass.jar javadriver.java
Replace
with the path to your MATLAB or MATLAB Runtime installation folder. For example, on Linux®, the path may bematlabroot
/usr/local/MATLAB/R2024b
.Run the
javadriver
application.On Windows, type:
java -classpath .;"
matlabroot
\toolbox\javabuilder\jar\javabuilder.jar";.\MyMATLABClass.jar javadriverOn Linux, type:
java -classpath .:"
matlabroot
/toolbox/javabuilder/jar/javabuilder.jar":.\MyMATLABClass.jar javadriverNote
If you are running the application on the Mac 64-bit platform, you must add the
-d64
flag in the Java command.
The javadriver
program displays the following
output:
--- USE: Constructors --- 1 2.2361 --- Done. ---
See Also
compiler.build.javaPackage
| Library Compiler