Assign Multiple MATLAB Functions to Java Class
This example shows you how to create a Java® matrix math program using multiple MATLAB® functions.
In this example, you perform the following steps:
Assign more than one MATLAB function to a generated class.
Manually handle native memory management.
Access the MATLAB functions in a Java application (
getfactor.java) by instantiatingFactorand using theMWArrayclass library to handle data conversion.Build and run the
MatrixMathDemoAppapplication.
MatrixMathApp Application
The MatrixMathApp application performs Cholesky, LU, and QR
factorizations on a simple tridiagonal matrix (finite difference matrix) with the
following form:
A = [ 2 -1 0 0 0
-1 2 -1 0 0
0 -1 2 -1 0
0 0 -1 2 -1
0 0 0 -1 2 ]
You supply the size of the matrix on the command line, and the program constructs the matrix and performs the three factorizations. The original matrix and the results are printed to standard output. You may optionally perform the calculations using a sparse matrix by specifying the string "sparse" as the second parameter on the command line.
Files
| MATLAB Functions | cholesky.m
ludecomp.mqrdecomp.m |
| MATLAB Function Location | |
| Java Code Location | |
Procedure
Copy the
MatrixMathExamplefolder that ships with MATLAB to your work folder:copyfile(fullfile(matlabroot,'toolbox','javabuilder','Examples','MatrixMathExample'),'MatrixMathExample')
At the MATLAB command prompt, navigate to the new
MatrixMathExample\MatrixMathDemoCompsubfolder in your work folder.If you have not already done so, set up your Java development environment. For details, see Configure Your Environment for Generating Java Packages.
Examine the MATLAB functions
cholesky.m,ludecomp.m, andqrdecomp.m.function [L] = Cholesky(A) L = chol(A);function [L,U] = LUDecomp(A) [L,U] = lu(A);function [Q,R] = QRDecomp(A) [Q,R] = qr(A);Build the Java package with the Java Package Compiler app or
compiler.build.javaPackageusing the following information:Field Value Java Package Name factormatrixClass Name factorFiles to Compile choleskyludecompqrdecompFor example, if you are using
compiler.build.javaPackage, type:buildResults = compiler.build.javaPackage(["cholesky.m","ludecomp.m","qrdecomp.m"], ... 'PackageName','factormatrix', ... 'ClassName','factor');
For more details, see the instructions in Generate Java Package and Build Java Application.
Write source code for an application that accesses the MATLAB functions.
The sample application for this example is in
MatrixMathExample\MatrixMathDemoJavaApp\getfactor.java.This statement creates an instance of the class
factor:theFactor = new factor();
The following statements call the methods that encapsulate the MATLAB functions:
result = theFactor.cholesky(1, a); ... result = theFactor.ludecomp(2, a); ... result = theFactor.qrdecomp(2, a); ...
In MATLAB, navigate to the
MatrixMathDemoJavaAppfolder.Copy the generated
factormatrix.jarpackage into this folder.For example, if you used
compiler.build.javaPackage, type:copyfile(fullfile('..','MatrixMathDemoComp','factormatrixjavaPackage','factormatrix.jar'))
In a command prompt window,
cdto theMatrixMathDemoJavaAppfolder.Compile the
getfactorapplication usingjavac.On Windows®, type:
javac -classpath "matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\factormatrix.jar getfactor.javaOn UNIX®, type:
javac -classpath "matlabroot/toolbox/javabuilder/jar/javabuilder.jar":./factormatrix.jar getfactor.java
Replace
with the path to your MATLAB or MATLAB Runtime installation folder. For example, on Linux®, the path may bematlabroot/usr/local/MATLAB/R2025b.Run the
getfactorapplication using a nonsparse matrix.On Windows, type:
java -classpath .;"matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\factormatrix.jar getfactor 4On UNIX, type:
java -classpath .:"matlabroot/toolbox/javabuilder/jar/javabuilder.jar":./factormatrix.jar getfactor 4
Note
If you are running the application on the Mac 64-bit platform, you must add the
-d64flag in the Java command.
To run the same program for a sparse matrix, use the same command and add the string
sparse at the end. For example, on Windows, type:
java -classpath .;"matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\factormatrix.jar getfactor 4 sparseUnderstanding the getfactor Program
The getfactor program takes one or two arguments from standard
input. The first argument is converted to the integer order of the test matrix. If the
string sparse is passed as the second argument, a sparse matrix is
created to contain the test array. The Cholesky, LU, and QR factorizations are then
computed and the results are displayed to standard output.
The main method has three parts:
The first part sets up the input matrix, creates a new factor object, and calls the
cholesky,ludecomp, andqrdecompmethods. This part is executed inside of atryblock, so that if an exception occurs during execution, the correspondingcatchblock will be executed.The second part is the
catchblock. The code prints a message to standard output to let the user know about the error that has occurred.The third part is a
finallyblock to manually clean up native resources before exiting.
See Also
compiler.build.javaPackage | Java Package
Compiler
