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 instantiatingFactor
and using theMWArray
class library to handle data conversion.Build and run the
MatrixMathDemoApp
application.
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.m qrdecomp.m |
MATLAB Function Location |
|
Java Code Location |
|
Procedure
Copy the
MatrixMathExample
folder 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\MatrixMathDemoComp
subfolder 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 Library Compiler app or
compiler.build.javaPackage
using the following information:Field Value Library Name factormatrix
Class Name factor
Files to Compile cholesky
ludecomp
qrdecomp
For 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
MatrixMathDemoJavaApp
folder.Copy the generated
factormatrix.jar
package into this folder.If you used
compiler.build.javaPackage
, type:copyfile(fullfile('..','MatrixMathDemoComp','factormatrixjavaPackage','factormatrix.jar'))
If you used the Library Compiler, type:
copyfile(fullfile('..','MatrixMathDemoComp','factormatrix','for_testing','factormatrix.jar'))
In a command prompt window,
cd
to theMatrixMathDemoJavaApp
folder.Compile the
getfactor
application 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/R2024b
.Run the
getfactor
application 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
-d64
flag 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 sparse
Understanding 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
, andqrdecomp
methods. This part is executed inside of atry
block, so that if an exception occurs during execution, the correspondingcatch
block will be executed.The second part is the
catch
block. The code prints a message to standard output to let the user know about the error that has occurred.The third part is a
finally
block to manually clean up native resources before exiting.
See Also
compiler.build.javaPackage
| Library Compiler