Block Console Display When Creating Figures in Java
This example shows how to use waitForFigures from a Java® application that you create using MATLAB®
Compiler SDK™. The object encapsulates MATLAB code that draws a simple plot.
Create a MATLAB function named
drawplot.mwith the following code:drawplot.m function drawplot() plot(1:10);
Build the Java package with the Java Package Compiler app or
compiler.build.javaPackageusing the following information:Field Value Library Name examplesClass Name PlotterFile to Compile drawplot.mFor example, if you are using
compiler.build.javaPackage, type:buildResults = compiler.build.javaPackage('drawplot.m', ... 'PackageName','examples', ... 'ClassName','Plotter');
For more details, see the instructions in Generate Java Package and Build Java Application.
Create a Java program in a file named
runplot.javawith the following code:import com.mathworks.toolbox.javabuilder.*; import examples.Plotter; public class runplot { public static void main(String[] args) { try { plotter p = new Plotter(); try { p.drawplot(); p.waitForFigures(); } finally { p.dispose(); } } catch (MWException e) { e.printStackTrace(); } } }In MATLAB, copy the generated
examples.jarpackage into your current folder. For instance, if you usedcompiler.build.javaPackage, type this commandcopyfile(fullfile('examplesjavaPackage','examples.jar'))
In a command prompt window, navigate to your work folder.
Compile the application using
javac.On Windows®, type:
javac -classpath "matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\examples.jar runplot.javaOn UNIX®, type:
javac -classpath "matlabroot/toolbox/javabuilder/jar/javabuilder.jar":./examples.jar runplot.java
Replace
with the path to your MATLAB or MATLAB Runtime installation folder. For example, on Windows, the path may bematlabrootC:\Program Files\MATLAB\R2025b.Run the application.
On Windows, type:
java -classpath .;"matlabroot\toolbox\javabuilder\jar\javabuilder.jar";.\examples.jar runplotOn UNIX, type:
java -classpath .:"matlabroot/toolbox/javabuilder/jar/javabuilder.jar":./examples.jar runplot
The program displays a plot from 1 to 10 in a MATLAB figure window. The application ends when you dismiss the figure.
To see what happens without the call to
waitForFigures, comment out the call, rebuild the application, and run it. In this case, the figure is drawn and immediately closes as the application exits.