The purpose of the example is to show you how to do the following:
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 (
Access the MATLAB function in a Java application
createplot.java) by instantiating the
and using the
MWArray class library to handle data
For complete reference information about the
hierarchy, see the
Build and run the
drawplot.m function displays a plot of
If you have not already done so, copy the files for this example as follows:
Copy the following folder that ships with MATLAB to your work folder:
At the MATLAB command prompt,
PlotExample subfolder in your work folder.
If you have not already done so, set the environment variables that are required on a development machine. See Configure Your Java Environment.
drawplot.m function as you
would any MATLAB function.
The following code defines the
function drawplot(x,y) plot(x,y);
This code is already in your work folder in
While in MATLAB, issue the following command to open the Library Compiler app:
You create a Java package by using the Library Compiler app to build a Java class that wraps around your MATLAB code. For an example, see Generate a Java Package and Build a Java Application.
Use the following information for your project:
|File to compile|
Write source code for an application that accesses the MATLAB function.
The sample application for this example is in
The program graphs a simple parabola from the equation y = x2 .
The program listing is shown here.
The program does the following:
Creates two arrays of double values, using
represent the data needed to plot the equation.
thePlot object, as shown:
thePlot = new plotter();
drawplot method to plot
the equation using the MATLAB
try-catch block to catch
and handle any exceptions.
javac. When entering this command, ensure
there are no spaces between path names in the
For example, there should be no space between
the following example.
cd to your work folder.
createplot.java is in your work folder
On Windows®, execute this command:
javac -classpath .;
matlabroot\toolbox\javabuilder\jar\javabuilder.jar; .\distrib\plotdemo.jar createplot.java
On UNIX®, execute this command:
javac -classpath .:matlabroot/toolbox/javabuilder/jar/javabuilder.jar: ./distrib/plotdemo.jar createplot.java
Run the application.
To run the
createplot.class file, do one
of the following:
On Windows, type:
java -classpath .;matlabroot\toolbox\javabuilder\jar\javabuilder.jar; .\distrib\plotdemo.jar createplot
On UNIX, type:
java -classpath .:matlabroot/toolbox/javabuilder/jar/javabuilder.jar: ./distrib/plotdemo.jar createplot
You should be using the same major version of Java that ships with MATLAB. To find out what version of Java MATLAB is running, enter the following MATLAB command:
If you are running on the Mac 64-bit platform, you must
-d64 flag in the Java command. See Limitations of the MATLAB Compiler SDK Java Target for more
createplot program should display the