Main Content

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:

  1. Assign more than one MATLAB function to a generated class.

  2. Manually handle native memory management.

  3. Access the MATLAB functions in a Java application ( by instantiating Factor and using the MWArray class library to handle data conversion.

  4. 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.


MATLAB Functionscholesky.m
MATLAB Function Locationmatlabroot\toolbox\javabuilder\Examples\MatrixMathExample\MatrixMathDemoComp\
Java Code Locationmatlabroot\toolbox\javabuilder\Examples\MatrixMathExample\MatrixMathDemoJavaApp\


  1. Copy the MatrixMathExample folder that ships with MATLAB to your work folder:


    At the MATLAB command prompt, navigate to the new MatrixMathExample\MatrixMathDemoComp subfolder in your work folder.

  2. If you have not already done so, set up your Java development environment. For details, see Configure Your Java Environment for Generating Packages.

  3. Examine the MATLAB functions cholesky.m, ludecomp.m, and qrdecomp.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);

  4. Build the Java package with the Library Compiler app or using the following information:

    Library Namefactormatrix
    Class Namefactor
    Files to Compilecholesky    ludecomp    qrdecomp

    For example, if you are using, type:

    buildResults =["cholesky.m","ludecomp.m","qrdecomp.m"], ...
    'PackageName','factormatrix', ...

    For more details, see the instructions in Generate Java Package and Build Java Application.

  5. Write source code for an application that accesses the MATLAB functions.

    The sample application for this example is in MatrixMathExample\MatrixMathDemoJavaApp\

    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);
  6. In MATLAB, navigate to the MatrixMathDemoJavaApp folder.

  7. Copy the generated factormatrix.jar package into this folder.

    • If you used, type:

    • If you used the Library Compiler, type:

  8. In a command prompt window, cd to theMatrixMathDemoJavaApp folder.

  9. Compile the getfactor application using javac.

    • On Windows®, type:

      javac -classpath "matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\factormatrix.jar
    • On UNIX®, type:

      javac -classpath "matlabroot/toolbox/javabuilder/jar/javabuilder.jar":./factormatrix.jar

    Replace matlabroot with the path to your MATLAB or MATLAB Runtime installation folder. For example, on Linux®, the path may be /usr/local/MATLAB/R2021b.

  10. Run the getfactor application using a nonsparse matrix.

    • On Windows, type:

      java -classpath .;"matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\factormatrix.jar getfactor 4
    • On UNIX, type:

      java -classpath .:"matlabroot/toolbox/javabuilder/jar/javabuilder.jar":./factormatrix.jar getfactor 4


    If you are running the application on the Mac 64-bit platform, you must add the -d64 flag in the Java command.

 Output for Nonsparse Matrix

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

 Output for Sparse Matrix

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, and qrdecomp methods. This part is executed inside of a try block, so that if an exception occurs during execution, the corresponding catch 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


Related Topics