Main Content

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:

  1. Use MATLAB Compiler SDK™ to create a package containing a class that has a private method that is automatically encapsulated.

  2. Access the MATLAB functions in a Java application, including use of the MWArray class hierarchy to represent data.

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


MATLAB Functionscomputefft.m
MATLAB Function Locationmatlabroot\toolbox\javabuilder\Examples\SpectraExample\SpectraDemoComp
Java Code Locationmatlabroot\toolbox\javabuilder\Examples\SpectraExample\SpectraDemoJavaApp\


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


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

  2. Examine the MATLAB functions computefft.m and plotfft.m.



  3. Build the Java package with the Library Compiler app or

    Use the following information for your project:

    Project Namespectralanalysis
    Class Namefourier
    File to Compileplotfft.m


    In this example, the application that uses the fourier class does not call computefft directly. The computefft method is required only by the plotfft method. You do not need to manually add the computefft function to the package, as the compiler automatically includes it during dependency analysis.

    For example, if you are using, type:

    buildResults ='plotfft.m', ...
    'PackageName','spectralanalysis', ...

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

  4. Write source code for a Java application that accesses the MATLAB functions.

    The sample application for this example is in SpectraExample\SpectraDemoJavaApp\

    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

      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

  5. In MATLAB, navigate to the SpectraDemoJavaApp folder.

  6. Copy the generated spectralanalysis.jar package into this folder.

    • If you used, type:

    • If you used the Library Compiler, type:

  7. Open a command prompt window and navigate to the SpectraDemoJavaApp folder.

  8. Compile the application using javac.

    • On Windows®, execute the following command:

      javac -classpath "matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\spectralanalysis.jar
    • On UNIX®, execute the following command:

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

    Replace matlabroot with the path to your MATLAB or MATLAB Runtime installation folder. For example, on Windows, the path may be C:\Program Files\MATLAB\R2021b.

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


    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:

    Time domain signal plotted above the power spectral density plot that reveals signals at 16 and 40 Hz

See Also


Related Topics