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 ( by instantiating the plotter class and using the MWArray class library to handle data conversion.


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

  3. Build and run the application.


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


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


    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)

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

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

    Project Nameplotdemo
    Class Nameplotter
    File to Compiledrawplot.m

    For example, if you are using, type:

    buildResults ='drawplot.m', ...
    'PackageName','plotdemo', ...

    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\

    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

    • 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, type:

    • If you used the Library Compiler, type:

  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
    • On UNIX®, execute this command:

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