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.

Files

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

Procedure

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

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

     computefft.m

     plotfft.m

  3. Build the Java package with the Library Compiler app or compiler.build.javaPackage.

    Use the following information for your project:

    Project Namespectralanalysis
    Class Namefourier
    File to Compileplotfft.m

    Note

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

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

    The sample application for this example is in SpectraExample\SpectraDemoJavaApp\powerspect.java.

     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 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 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'))
  7. Open a command prompt window and navigate to the SpectraDemoJavaApp folder.

  8. Compile the powerspect.java application using javac.

    • 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 matlabroot with the path to your MATLAB or MATLAB Runtime installation folder. For example, on Windows, the path may be C:\Program Files\MATLAB\R2024b.

  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
      

    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:

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

See Also

|

Related Topics