Main Content

Block Console Display When Creating Figures

WaitForFiguresToDie Method

The MATLAB® Compiler SDK™ product adds a WaitForFiguresToDie method to each .NET class that it creates.

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.

WaitForFiguresToDie takes no arguments. Your application can call WaitForFiguresToDie any time during execution.

Tip

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

Caution

Calling WaitForFiguresToDie 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 component 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 named drawplot.m:

    function drawplot()
    plot(1:10);
    
  3. Build the .NET component with the Library Compiler app or compiler.build.dotNETAssembly using the following information:

    FieldValue
    Library NameFigure
    Class NamePlotter
    File to Compiledrawplot.m

    For example, if you are using compiler.build.dotNETAssembly, type:

    buildResults = compiler.build.dotNETAssembly('drawplot.m', ...
    'AssemblyName','Figure', ...
    'ClassName','Plotter');

    For more details, see the instructions in Generate .NET Assembly and Build .NET Application.

  4. In Visual Studio®, create a C# Console App (.NET Framework). Replace the generated source code 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. Add a reference to your generated assembly file Figure.dll.

  6. Compile and 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.

See Also

| |

Related Topics