| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB Builder JA |
| Contents | Index |
| Learn more about MATLAB Builder JA |
| On this page… |
|---|
MATLAB Functions to Be Encapsulated |
The purpose of the example is to show you the following:
How to assign more than one MATLAB function to a component class.
How to manually handle native memory management.
How to access the component in a Java application (getfactor.java) by instantiating Factor and using the MWArray class library to handle data conversion.
How to build and run the MatrixMathDemoApp application
This example builds a Java component to perform matrix math. The example creates a program that 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.
The following code defines the MATLAB functions used in the example:
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. This is done 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.
Step-by-Step Procedure
If you have not already done so, copy the files for this example as follows:
Copy the following folder that ships with MATLAB to your work folder:
matlabroot\toolbox\javabuilder\Examples\MatrixMathExample
At the MATLAB command prompt, cd to the new MatrixMathExample subfolder in your work folder.
If you have not already done so, set the environment variables that are required on a development machine. See Settings for Environment Variables (Development Machine).
Write the MATLAB functions as you would any MATLAB function.
The code for the cholesky, ludecomp, and qrdecomp functions is already in your work folder in MatrixMathExample\MatrixMathDemoComp\.
While in MATLAB, issue the following command to open the Deployment Tool window:
deploytool
You create a Java application by using the Deployment Tool GUI to build a Java class that wraps around your M-code.
To compile or build the Java application using the Deployment Tool, use the following information as you work through this example in Building the Java Component:
| Project Name | factormatrix |
| Class Name | factor |
| Files to compile | cholesky.mludecomp.mqrdecomp.m |
Write source code for an application that accesses the component.
The sample application for this example is in MatrixMathExample\MatrixMathDemoJavaApp\getfactor.java.
The program listing is shown here.
The statement:
theFactor = new factor();
creates an instance of the class 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); ...
Compile the getfactor application using javac. When entering this command, ensure there are no spaces between path names in the matlabroot argument. For example, there should be no space between javabuilder.jar; and .\distrib\factormatrix.jar in the following example.
cd to the matlabroot\factormatrix folder. Ensure getfactor.java is in this folder.
On Windows, execute the following command:
javac -classpath .;matlabroot\toolbox\javabuilder\jar\javabuilder.jar; .\distrib\factormatrix.jar getfactor.java
On UNIX, execute the following command:
javac -classpath .:matlabroot/toolbox/javabuilder/jar/javabuilder.jar: ./distrib/factormatrix.jar getfactor.java
Run the application.
Run getfactor using a nonsparse matrix
On Windows, execute the getfactor class file as follows:
java -classpath .;matlabroot\toolbox\javabuilder\jar\javabuilder.jar; .\distrib\factormatrix.jar getfactor 4
On UNIX, execute the getfactor class file as follows:
java -classpath .:matlabroot/toolbox/javabuilder/jar/javabuilder.jar: ./distrib/factormatrix.jar getfactor 4
Note The supported JRE version is 1.6.0. To find out what JRE you are using, refer to the output of 'version -java' in MATLAB or refer to the jre.cfg file in matlabroot/sys/java/jre/<arch> or mcrroot/sys/java/jre/<arch>. |
Note If you are running on Mac or Solaris 64-bit platforms, you must add the -d64 flag in the Java command. See Limitations and Restrictions for more specific information. |
Output for the Matrix Math Example
To run the same program for a sparse matrix, use the same command and add the string sparse to the command line:
java (... same arguments) getfactor 4 sparse
![]() | Spectral Analysis Example | Phonebook Example | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |