Main Content

Block Execution of Applications That Create Figures


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

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

  • There are one or more figures open that were created by an instance of a COM object created by the compiler.

  • 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 MCRWaitForFigures is called, execution of the calling program is blocked if any figures created by the calling object remain open.


Be careful when calling the MCRWaitForFigures method. Calling this method from a Microsoft® Visual Basic® UI or from an interactive program such as Microsoft Excel® can hang the application. This method should be called only from console-based programs.

Use MCRWaitForFigures to Block Execution

The following example illustrates using MCRWaitForFigures from a Microsoft Visual C++® console application. The example uses a COM object created by the compiler; 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. Create the following MATLAB file in this folder:

    function drawplot()
  3. Use the compiler to create a COM component with the following properties:

    Component nameplotdemo
    Class nameplotdemoclass


    Instead of using the Library Compiler app, you can create the component by issuing the following command at the MATLAB prompt:

    mcc -d 'D:\work\plotdemo\src' -v -B    'ccom:plotdemo,plotdemoclass,1.0' 
  4. Create a Visual C++ program in a file named runplot.cpp with the following code:

    #include "src\plotdemo_idl.h" 
    #include "src\plotdemo_idl_i.c"
    int main()
        // Initialize the COM library     
        HRESULT hr = CoInitialize(NULL);   
        // Create an instance of the COM object you created    
        Iplotdemoclass* pIplotdemoclass = NULL;     
        hr = CoCreateInstance(CLSID_plotdemoclass, NULL, 
             CLSCTX_INPROC_SERVER, IID_Iplotdemoclass, 
             (void **)&pIplotdemoclass); 
        // Call the drawplot method
        hr = pIplotdemoclass->drawplot();
        // Block execution until user dismisses the figure window
        hr = pIplotdemoclass->MCRWaitForFigures();
        // Uninitialize COM     
        return 0;
  5. In the MATLAB Command Window, build the application as follows:

    mbuild runplot.cpp

    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.


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