Display MATLAB Plot in Java Application
In this example, you integrate a MATLAB® function into a Java® application by performing these steps:
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
).Access the MATLAB function in a Java application (
createplot.java
) by instantiating theplotter
class and using theMWArray
class library to handle data conversion.Note
For complete reference information about the
MWArray
class hierarchy, see thecom.mathworks.toolbox.javabuilder
package.Build and run the
createplot.java
application.
Files
MATLAB Function Location |
|
Java Code Location |
|
Procedure
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.Examine the
drawplot.m
function.function drawplot(x,y) plot(x,y);
The function displays a plot of input parameters
x
andy
.Create a Java package by using the Library Compiler app or
compiler.build.javaPackage
using the following information:Project Name plotdemo
Class Name plotter
File to Compile drawplot.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.
Write source code for a Java application that accesses the MATLAB function.
The sample application for this example is in
PlotExample\PlotDemoJavaApp\createplot.java
.The program does the following:
Creates two arrays of double values
x
andy
usingMWNumericArray
to represent the equation y = x2Instantiates the
plotter
class asthePlot
objectthePlot = new plotter();
Calls the
drawplot
method to plot a simple parabola using the MATLABplot
functionthePlot.drawplot(x,y);
Uses a
try-catch
block to catch and handle any exceptions
In MATLAB, navigate to the
PlotDemoJavaApp
folder.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'))
In a command prompt window, navigate to the
PlotDemoJavaApp
folder where you copiedplotdemo.jar
.Compile the
createplot
application usingjavac
.On Windows®, execute this command:
javac -classpath "
matlabroot
\toolbox\javabuilder\jar\javabuilder.jar";.\plotdemo.jar createplot.javaOn UNIX®, execute this command:
javac -classpath "
matlabroot
/toolbox/javabuilder/jar/javabuilder.jar":./plotdemo.jar createplot.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
createplot
application.On Windows, type:
java -classpath .;"
matlabroot
\toolbox\javabuilder\jar\javabuilder.jar";.\plotdemo.jar createplotOn UNIX, type:
java -classpath .:"
matlabroot
/toolbox/javabuilder/jar/javabuilder.jar":./plotdemo.jar createplot
The
createplot
program displays the following output.
See Also
compiler.build.javaPackage
| Library Compiler