| 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… |
|---|
The purpose of the example is to show you the following:
How to use the MATLAB Builder JA product to create a component (spectralanalysis) containing a class that has a private method that is automatically encapsulated
How to access the component in a Java application (powerspect.java), including use of the MWArray class hierarchy to represent data
How to build and run the application
The component spectralanalysis analyzes a signal and graphs the result. The class, fourier, performs a fast Fourier transform (FFT) on an input data array. A method of this class, computefft, returns the results of that FFT as two output arrays—an array of frequency points and the power spectral density. The second method, plotfft, graphs the returned data. These two methods, computefft and plotfft, encapsulate MATLAB functions.
The MATLAB code for these two methods is in computefft.m and plotfft.m, which is found in matlabroot\toolbox\javabuilder\Examples\SpectraExample\SpectraDemoComp.
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\SpectraExample
At the MATLAB command prompt, cd to the new SpectraExample 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 M-code that you want to access.
This example uses computefft.m and plotfft.m, which are already in your work folder in SpectraExample\SpectraDemoComp.
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 | spectralanalysis |
| Class Name | fourier |
| File to compile | plotfft.m |
Write source code for an application that accesses the component.
The sample application for this example is in SpectraExample\SpectraDemoJavaApp\powerspect.java.
The program listing is shown here.
The program does the following:
Constructs an input array with values representing a random signal with two sinusoids at 15 and 40 Hz embedded inside of it
Creates an MWNumericArray array that contains the data, as shown:
data = MWNumericArray.newInstance(dims, MWClassID.DOUBLE, MWComplexity.REAL);
Instantiates a fourier object
Calls the plotfft method, which calls computeftt and plots the data
Uses a try-catch block to handle exceptions
Frees native resources using MWArray methods
Compile the powerspect.java 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\spectralanalysis.jar in the following example.
Open a Command Prompt window and cd to the matlabroot\spectralanalysis folder. cd to your work folder. Ensure powerspect.java is in your work folder
On Windows, execute the following command:
javac -classpath .;matlabroot\toolbox\javabuilder\jar\javabuilder.jar; .\distrib\spectralanalysis.jar powerspect.java
On UNIX, execute the following command:
javac -classpath .:matlabroot/toolbox/javabuilder/jar/javabuilder.jar: ./distrib/spectralanalysis.jar powerspect.java
Run the application.
On Windows, execute the powerspect class file:
java -classpath .;matlabroot\toolbox\javabuilder\jar\javabuilder.jar .\distrib\spectralanalysis.jar powerspect
On UNIX, execute the powerspect class file:
java -classpath .:matlabroot/toolbox/javabuilder/jar/javabuilder.jar: ./distrib/spectralanalysis.jar powerspect
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. |
The powerspect program should display the output:

![]() | Plot Example | Matrix Math 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 |