Block Console Display When Creating Figures

WaitForFiguresToDie Method

The MATLAB® Compiler SDK™ product adds a WaitForFiguresToDie method to each .NET class that it creates. WaitForFiguresToDie takes no arguments. Your application can call WaitForFiguresToDie any time during execution.

The purpose of WaitForFiguresToDie is to block execution of a calling program as long as figures created in encapsulated MATLAB code are displayed. Typically you use WaitForFiguresToDie when:

  • There are one or more figures open that were created by a .NET assembly created by the MATLAB Compiler SDK product.

  • The method that displays the graphics requires user input before continuing.

  • The method that calls the figures was called from main() in a console program.

When WaitForFiguresToDie is called, execution of the calling program is blocked if any figures created by the calling object remain open.

Tip

Consider using the console.readline method when possible as it accomplishes much of this functionality in a standardized manner.

Caution

Use caution when calling the WaitForFiguresToDie method. Calling this method from an interactive program can make the application stop responding. This method should be called only from console-based programs.

Using WaitForFiguresToDie to Block Execution

The following example illustrates using WaitForFiguresToDie from a .NET application. The example uses a .NET assembly 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 MATLAB Compiler SDK to create a .NET assembly with the following properties:

    Assembly nameFigure
    Class namePlotter
  4. Create a .NET program in a file named runplot with the following code:

    using Figure.Plotter;
    
    public class Main 
    {
      public static void main(String[] args) 
      {
        try
        {
          plotter p = new Plotter();
          try 
          {
            p.drawplot();
            p.WaitForFiguresToDie();
          }
          catch (Exception e)
          {
            console.writeline(e);
          }
        }
      }
    }
  5. Compile the application.

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

    Note

    To see what happens without the call to WaitForFiguresToDie, 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.

Was this topic helpful?