| MATLAB Builder for Java | ![]() |
www.kxcad.net Home > CAE Software Index > MATLAB Index >
This example demonstrates how to create a buffered image from a graphical representation of the surf(peaks) function in MATLAB.
The hardcopy function is used to output the figure window as an array:
function w = getSurfsFigure f = figure; set(f,'Visible', 'off'); f = surf(peaks); w = hardcopy(gcf,'-dOpenGL','-r0'); end
Note There is minimal error handling in this example. Integrate this code with whatever logging is currently in place for your Java layer. |
Note Be aware that hardcopy is currently an undocumented function and subject to change. |
Create a Java object from the function above by doing the following:
Start the Deployment Tool by entering deploytool from the MATLAB Command Prompt.
Select File > New > Deployment Project from the MATLAB interface.
Select a MATLAB Builder for Java Project and Java Package, then give the project a name and location. Click OK.
Click Settings in the Deployment Tool and enter the package name as com.mathworks.deploy.peaks. Click OK.
In the Deployment Tool, change the project_nameclass name to Peaks by right-clicking on the class folder and selecting Rename.
Add the function getSurfsFigure.m to the Peaks class by dragging the function from the MATLAB Current Directory browser to the Peaks class folder in the Deployment Tool.
Click the Build icon in the Deployment Tool toolbar and build your Java object. Be sure to select the Include MCR build option.
From the output distrib directory of your build, copy peaks.jar to the directory where you are building your application.
Create the SurfPeaks.java code:
//imported classes from JRE
import java.awt.Image;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
//imported classes from javabuilder.jar
import com.mathworks.toolbox.javabuilder.Images;
import com.mathworks.toolbox.javabuilder.MWArray;
import com.mathworks.toolbox.javabuilder.MWNumericArray;
import com.mathworks.toolbox.javabuilder.MWException;
//Import for the Deployment Project peaks.jar
import com.mathworks.deploy.peaks.Peaks;
//The Goal of this class is to show how you would deal with
// static images coming from a java deployment of a matlab figure.
//This can be run as a stand alone java application.
//
//The matlab function being deployed is surf(peaks).
//The hardcopy function is used to
// output the figure window as an array.
//M Code:
//********************************************
// function w = getSurfsFigure
// f = figure;
// surf(peaks);
// w = hardcopy(gcf,'-dOpenGL','-r0');
// close(f);
// end
//********************************************
//
//For this example you must have the deployment project
// jar and the javabuilder.jar on your classpath.
//
//Note:
//For this example there is minimal error handling.
//Typically you would want to integrate this with whatever
// logging is currently in place for your java layer.
public class SurfPeaks
{
//This initializes and executes the JFrame.
public static void main(String args[])
{
ImageIcon icon = new ImageIcon(getSurfImage());
JLabel label = new JLabel(icon);
JFrame frame = new JFrame();
frame.setSize(icon.getIconWidth(),icon.getIconHeight());
frame.setContentPane(label);
frame.setVisible(true);
}
//This method is basically our "business logic" method.
//It is responsible for instantiating our Matlab deployment,
//passing in any needed inputs, and dealing with any outputs.
//In this example we have no inputs, and the only output is the
//figure in hardcopy format (three dimensional MWNumericArray)
private static Image getSurfImage()
{
try
{
//Our deployment uses native resources and
//should be disposed of as soon as possible.
Peaks matlabModel = new Peaks();
try
{
//If we had any inputs to our method
//they would be passed in here.
Object[] results = matlabModel.getSurfsFigure(1);
//This array uses native resources and
//should be disposed of as soon as possible.
MWArray mwArray = (MWArray)results[0];
try
{
//Since we want this method to return only
// non matlab data
// we convert the matlab figure to a
// buffered image and return it.
return Images.renderArrayData
((MWNumericArray)mwArray);
}
finally
{
MWArray.disposeArray(mwArray);
}
}
finally
{
matlabModel.dispose();
}
}
catch(MWException mwe)
{
mwe.printStackTrace();
return null;
}
}
}
Compile the program using javac and the following command:
javac -classpath javabuilder.jar;peaks.jar SurfPeaks.java
Ensure that javabuilder.jar and peaks.jar (compiled in an earlier step) are both in the directory you compile from (or define their full paths).
Run SurfPeaks.class using the following java command. Ensure that javabuilder.jar and peaks.jar (compiled in an earlier step) are both in the directory you compiled in (or define their full paths).
java -classpath javabuilder.jar;peaks.jar;. SurfPeaks
The following Surf Peaks graphic should open:

| Phonebook Example | Optimization Example | ![]() |
© 1984-2007 The MathWorks, Inc. Terms of Use Patents Trademarks Acknowledgments