Creating Graphical User Interfaces Previous page   

ActiveX Controls

You can insert an ActiveX control into your GUI if you are running MATLAB on Microsoft Windows. When you drag an ActiveX component from the component palette into the layout area, GUIDE displays a dialog in which you can select any registered ActiveX control on your system. When you select an ActiveX control and click Create, the control appears as a small box in the Layout Editor. You can then program the control to do what you want it to.

See MATLAB COM Client Support in the online MATLAB documentation to learn more about ActiveX controls.

This section covers the following topics:

Adding an ActiveX Control to a GUI

This section shows how to add an ActiveX control to a GUI. The section describes a simple ActiveX control that displays a circle. After adding the ActiveX control, you can program the GUI to change the radius of the circle and display the new value of the radius.

To make the example,

  1. Open a new GUI in GUIDE and drag an ActiveX control from the component palette into the Layout Editor.
  2. Scroll down the ActiveX Control List and select Mwsamp Control. This displays a preview of the ActiveX control Mwsamp, as shown in the following figure.

    Select an Active x control

  3. Click Create to display the ActiveX control in the Layout Editor, and resize the control to approximately the size of the square shown in the preview pane. You can do this by clicking and dragging a corner of the control, as shown in the following figure.

    Resizing the control by clicking and dragging

  4. Click the Run button on the toolbar and save the GUI when prompted. GUIDE displays the GUI shown in the following figure.

    active x control

Viewing the ActiveX Properties with the Property Inspector

To view the properties of the ActiveX control, right-click the control in the Layout Editor and select Property Inspector, or select Property Inspector from the View menu. This displays the Property Inspector, as shown in the following figure.

Property inspector showing label and radius properties

The ActiveX control mwsamp has just two properties:

The example in the next section shows how to program the GUI to

Adding a Callback to an ActiveX Control to Change a Property

To change a property of the control when a user performs an action, you need to add a callback to an ActiveX control. For example, to make mwsamp decrease the radius of the circle by 10 percent and display the new value each time a user clicks the ActiveX control, you can add an event, or callback, corresponding to the click action. To do so,

  1. Right-click the ActiveX control in the Layout Editor to bring up its context menu.
  2. In the context menu, place the cursor over View Callbacks and select Click. This creates a callback called activex1_Click and opens the GUI M-file with the first line of the callback highlighted.
  3. Add the following commands to the activex1_Click callback:
  4. Add the following command to the opening function:
  5. Run the GUI.

Now, each time you click the ActiveX control, the radius of the circle is reduced by 10 percent and the new value of the radius is displayed. The following figure shows the GUI after clicking the circle six times.

GUI control for radius set to 9

If you click the GUI enough times, the circle disappears entirely.

Adding a Uicontrol to Change an ActiveX Control Property

You can also add other Uicontrols to the GUI to change the properties of an ActiveX control. For example, you can add a slider that changes the radius of the circle in the Mwsamp GUI. To do so,

  1. Drag a slider into the layout area.
  2. Right-click the slider and select M-file Editor.
  3. Add the following command to the slider1 callback:
  4. Add the following command to the opening function:
  5. Add the following command at the end of the code in the activex1_Click callback:
  6. Run the GUI.

When you move the slider by clicking and dragging, the radius changes to a new value between 0 and the default radius of 20, as shown in the following figure.

GUI control for radius of 14

The command

does the following:

Note that clicking the ActiveX control causes the slider to change position corresponding to the new value of the radius. The command

in the activex1_Click callback resets the slider's Value each time the user clicks the ActiveX control.

Viewing the Methods for an ActiveX Control

To view the available methods for the ActiveX control, you first need to obtain the handle to the control. One way to do this is the following:

  1. In the GUI M-file, add the command keyboard on a separate line of the activex1_Click callback. The command keyboard puts MATLAB in debug mode and pauses at the activex1_Click callback when you click the ActiveX control.
  2. Run the GUI and click the ActiveX control.

The handle to the control is now set to hObject. To view the methods for the control, enter

at the MATLAB prompt. This displays the available methods in a new window, as shown in the following figure.

Methods of class COM.mwsamp.mwsampctrol.1.release

Alternatively, you can enter

which displays the available methods in the Command Window.

For more information about methods for ActiveX controls, see Invoking Methods in the online MATLAB documentation. See the reference pages for methodsview and methods for more information about these functions.

Saving a GUI that Contains an ActiveX Control

When you save a GUI that contains ActiveX controls, GUIDE creates a file in the current directory for each such control. The filename consists of the name of the GUI followed by an underscore (_) and activexn, where n is a sequence number. For example, if the GUI is named mygui then the filename would be mygui_activex1. The filename does not have an extension.

Compiling a GUI that Contains an ActiveX Control

If you use the MATLAB Compiler mcc command to compile a GUI that contains an ActiveX control, you must use the -a flag to add the ActiveX file, which GUIDE saves in the current directory, to the CTF archive. Your command should be similar to this

where mygui_activex1 is the name of the ActiveX file. See the MATLAB Compiler documentation for more information. If you have more than one such file, use a separate -a flag for each file. You must have installed the MATLAB Compiler to compile a GUI.


Previous page Axes 

© 1994-2005 The MathWorks, Inc.