Create Java Application with Multiple MATLAB Functions
This example shows how to create a Java® application that uses multiple MATLAB® functions to analyze a signal and then graph the result.
In this example, you perform the following steps:
Use MATLAB Compiler SDK™ to create a package containing a class that has a private method that is automatically encapsulated.
Access the MATLAB functions in a Java application, including use of the
MWArray
class hierarchy to represent data.Build and run the application.
spectralanalysis
Application
The spectralanalysis
application 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.
Files
MATLAB Functions | computefft.m plotfft.m |
MATLAB Function Location |
|
Java Code Location |
|
Procedure
Copy the
SpectraExample
folder that ships with MATLAB to your work folder:copyfile(fullfile(matlabroot,'toolbox','javabuilder','Examples','SpectraExample'),'SpectraExample')
At the MATLAB command prompt, navigate to the new
SpectraExample\SpectraDemoComp
subfolder in your work folder.Examine the MATLAB functions
computefft.m
andplotfft.m
.Build the Java package with the Library Compiler app or
compiler.build.javaPackage
.Use the following information for your project:
Project Name spectralanalysis
Class Name fourier
File to Compile plotfft.m
Note
In this example, the application that uses the
fourier
class does not callcomputefft
directly. Thecomputefft
method is required only by theplotfft
method. You do not need to manually add thecomputefft
function to the package, as the compiler automatically includes it during dependency analysis.For example, if you are using
compiler.build.javaPackage
, type:buildResults = compiler.build.javaPackage('plotfft.m', ... 'PackageName','spectralanalysis', ... 'ClassName','fourier');
For more details, see the instructions in Generate Java Package and Build Java Application.
Write source code for a Java application that accesses the MATLAB functions.
The sample application for this example is in
SpectraExample\SpectraDemoJavaApp\powerspect.java
.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 datadata = MWNumericArray.newInstance(dims, MWClassID.DOUBLE, MWComplexity.REAL);
Instantiates a
fourier
objectCalls the
plotfft
method, which callscomputeftt
and plots the dataUses a
try
-catch
block to handle exceptionsFrees native resources using
MWArray
methods
In MATLAB, navigate to the
SpectraDemoJavaApp
folder.Copy the generated
spectralanalysis.jar
package into this folder.If you used
compiler.build.javaPackage
, type:copyfile(fullfile('..','SpectraDemoComp','spectralanalysisjavaPackage','spectralanalysis.jar'))
If you used the Library Compiler, type:
copyfile(fullfile('..','SpectraDemoComp','spectralanalysis','for_testing','spectralanalysis.jar'))
Open a command prompt window and navigate to the
SpectraDemoJavaApp
folder.Compile the
powerspect.java
application usingjavac
.On Windows®, execute the following command:
javac -classpath "matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\spectralanalysis.jar powerspect.java
On UNIX®, execute the following command:
javac -classpath "matlabroot/toolbox/javabuilder/jar/javabuilder.jar":./spectralanalysis.jar powerspect.java
Replace
with the path to your MATLAB or MATLAB Runtime installation folder. For example, on Windows, the path may bematlabroot
C:\Program Files\MATLAB\R2024b
.Run the
powerspect
application.On Windows, execute the following command:
java -classpath .;"matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\spectralanalysis.jar powerspect
On UNIX, execute the following command:
java -classpath .:"matlabroot/toolbox/javabuilder/jar/javabuilder.jar":./spectralanalysis.jar powerspect
Note
If you are running the application on the Mac 64-bit platform, you must add the
-d64
flag in the Java command.The
powerspect
program displays the following output:
See Also
compiler.build.javaPackage
| Library Compiler