Placing Controls in a Figure Window

Example Overview

In this example, a simple model displays the simulation time on a control located in a MATLAB® figure window.

To open a completed copy of the model, enter gauges_offblock in the MATLAB Command Window. Alternatively, follow the instructions below for building it yourself.

Creating Helper M-Files and Building the Model

  1. Create and execute an M-file called gauges_gaugewindow that consists of these statements.

    f = figure;
    h = actxcontrol('mwagauge.agaugectrl.1',[100 100 100 100],f);
    

    This M-file creates a figure window containing a Generic Angular Gauge, whose program ID is mwagauge.agaugectrl.1. The figure window looks like this (resized).

    The M-file also specifies the position of the control in the figure window. For more information about actxcontrol, see its reference documentation.

  2. Create an M-file called gauges_off_block that consists of these statements.

    function hactx = gauges_off_block
    hactx = evalin('base','h');
    

    The gauges_off_block function returns the handle of the control that is to be connected to the ActiveX Control block.

  3. In a new model window, connect a Clock block to an ActiveX Control block.

  4. Open the ActiveX Control block to modify its parameters as follows:

    1. Clear the In-block control check box. The number of fields on the dialog box changes as a result.

    2. In the Connections field, select input. When you apply this change later, the outport on the ActiveX Control block will disappear.

    3. In the Input property field, enter NeedleValue. During the simulation, the Generic Angular Gauge (that is, the control referenced by the block) sets the NeedleValue property to the value of the signal at the ActiveX Control block's inport.

        Note   If you adapt this example to use the Strip Chart control instead, then set Input property to Y. For other controls in this blockset, set Input property to the value used in the corresponding parameter field in the library block.

    4. In the Handle location field, enter gauges_off_block.

    As a result of these changes, the Block Parameters dialog box looks like this.

  5. Click OK. The gauges_off_block M-file executes and returns the handle of the control in the figure window.

  6. Run the simulation. Notice that the Generic Angular Gauge reflects the clock time.

Saving and Reopening the Model

If you want to use this model in a different MATLAB software session, then you must preserve both the model and the commands that create the figure window and gauge. Here is an easy way to do this:

  1. Save the model to give it a name.

  2. If the model's name is mymodel, then use these commands in the MATLAB Command Window to preserve the commands that create the figure window and gauge.

    set_param('mymodel','PreLoadFcn','gauges_gaugewindow');
    save_system
    

Now, opening mymodel automatically creates the figure that contains the gauge.

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS