Main Content

Display MATLAB Plot in Java Application

In this example, you integrate a MATLAB® function into a Java® application by performing these steps:

  1. Use the MATLAB Compiler SDK™ product to convert a MATLAB function (drawplot.m) to a method of a Java class (plotter) and wrap the class in a Java package (plotdemo).

  2. Access the MATLAB function in a Java application (createplot.java) by instantiating the plotter class and using the MWArray class library to handle data conversion.

    Note

    For complete reference information about the MWArray class hierarchy, see the com.mathworks.toolbox.javabuilder package.

  3. Build and run the createplot.java application.

Files

MATLAB Function Locationmatlabroot\toolbox\javabuilder\Examples\PlotExample\PlotDemoComp\drawplot.m
Java Code Locationmatlabroot\toolbox\javabuilder\Examples\PlotExample\PlotDemoJavaApp\createplot.java

Procedure

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

    copyfile(fullfile(matlabroot,'toolbox','javabuilder','Examples','PlotExample'),'PlotExample')

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

  2. Examine the drawplot.m function.

    function drawplot(x,y)
    plot(x,y);
    

    The function displays a plot of input parameters x and y.

  3. Create a Java package by using the Library Compiler app or compiler.build.javaPackage using the following information:

    Project Nameplotdemo
    Class Nameplotter
    File to Compiledrawplot.m

    For example, if you are using compiler.build.javaPackage, type:

    buildResults = compiler.build.javaPackage('drawplot.m', ...
    'PackageName','plotdemo', ...
    'ClassName','plotter');

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

    The sample application for this example is in PlotExample\PlotDemoJavaApp\createplot.java.

     createplot.java

    The program does the following:

    • Creates two arrays of double values x and y using MWNumericArray to represent the equation y = x2

    • Instantiates the plotter class as thePlot object

      thePlot = new plotter();
    • Calls the drawplot method to plot a simple parabola using the MATLAB plot function

      thePlot.drawplot(x,y);
      
    • Uses a try-catch block to catch and handle any exceptions

  5. In MATLAB, navigate to the PlotDemoJavaApp folder.

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

    • If you used compiler.build.javaPackage, type:

      copyfile(fullfile('..','PlotDemoComp','plotdemojavaPackage','plotdemo.jar'))
    • If you used the Library Compiler, type:

      copyfile(fullfile('..','PlotDemoComp','plotdemo','for_testing','plotdemo.jar'))
  7. In a command prompt window, navigate to the PlotDemoJavaApp folder where you copied plotdemo.jar.

  8. Compile the createplot application using javac.

    • On Windows®, execute this command:

      javac -classpath "matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\plotdemo.jar createplot.java
    • On UNIX®, execute this command:

      javac -classpath "matlabroot/toolbox/javabuilder/jar/javabuilder.jar":./plotdemo.jar createplot.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 createplot application.

    • On Windows, type:

      java -classpath .;"matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\plotdemo.jar createplot
    • On UNIX, type:

      java -classpath .:"matlabroot/toolbox/javabuilder/jar/javabuilder.jar":./plotdemo.jar createplot

    The createplot program displays the following output.

    Plot of y equals x squared

See Also

|

Related Topics