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 pacakage (plotdemo).
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 |
Build and run the createplot.java application.
The drawplot.m function displays a plot of
input parameters x and y.
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:
matlabroot\toolbox\javabuilder\Examples\PlotExample
At the MATLAB command prompt, cd to
the new 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.
Write the drawplot.m function as you
would any MATLAB function.
The following code defines the drawplot.m function:
function drawplot(x,y) plot(x,y);
This code is already in your work folder in PlotExample\PlotDemoComp\drawplot.m.
While in MATLAB, issue the following command to open the Library Compiler app:
libraryCompiler
You create a Java package by using the Library Compiler app to build a Java class that wraps around your MATLAB code.
To create the Java package the Library Compiler app, use the following information as you work through this example in Compile Java Packages with Library Compiler App:
| Project Name | plotdemo |
| Class Name | plotter |
| File to compile | drawplot.m |
Write source code for an application that accesses the MATLAB function.
The sample application for this example is in .matlabroot\toolbox\javabuilder\Examples\PlotExample\PlotDemoJavaApp\createplot.java
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 MWNumericArray to
represent the data needed to plot the equation.
Instantiates the plotter class
as thePlot object, as shown:
thePlot = new plotter();
Calls the drawplot method to plot
the equation using the MATLAB plot function,
as shown:
thePlot.drawplot(x,y);
Uses a try-catch block to catch
and handle any exceptions.
Compile the createplot application
using javac. When entering this command, ensure
there are no spaces between path names in the argument.
For example, there should be no space between matlabrootjavabuilder.jar; and .\distrib\plotdemo.jar in
the following example. cd to your work folder.
Ensure 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
Note: You should be using the same version of Java that ships with MATLAB. To find out what version of Java MATLAB is running, enter the following MATLAB command: version -java
|
Note:
If you are running on the Mac 64-bit platform, you must
add the |
The createplot program should display the
output.
