Main Content

Execution of Applications that Create Figures

The following example illustrates using waitForFigures from a Java® application. The example uses a Java class created by the MATLAB® Compiler SDK™ product; the object encapsulates MATLAB code that draws a simple plot.

  1. Create a work folder for your source code. In this example, the folder is D:\work\plotdemo.

  2. In this folder, create the following MATLAB file:

    drawplot.m
    
    function drawplot()
        plot(1:10);
    
  3. Use the compiler to create a Java package with the following properties:

    Package nameexamples
    Class namePlotter
  4. Create a Java program in a file named runplot.java with 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();
        }
      }
    }	
  5. Compile the application with the javac command.

    When you run the application, the program displays a plot from 1 to 10 in a MATLAB figure window. The application ends when you dismiss the figure.

    Note

    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 is immediately destroyed as the application exits.