Creating Graphical User Interfaces |
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.
Note If MATLAB is not installed locally on your computer -- for example, if you are running MATLAB over a network -- you might not find the ActiveX control described in this example. To register the control, see Registering Controls and Servers in the online MATLAB documentation. |
Mwsamp
, as shown in the following figure.
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.
The ActiveX control mwsamp
has just two properties:
Label
, which contains the text that appears at the top of the control
Radius
, the default radius of the circle, which is 20
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,
activex1_Click
and opens the GUI M-file with the first line of the callback highlighted.
activex1_Click
callback:
hObject.radius = .9*hObject.radius; hObject.label = ['Radius = ' num2str(hObject.radius)]; refresh(handles.figure1);
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.
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,
slider1
callback:
handles.activex1.radius = ... get(hObject, 'Value')*handles.default_radius; handles.activex1.label = ... ['Radius = ' num2str(handles.activex1.radius)]; refresh(handles.figure1);
activex1_Click
callback:
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.
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:
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.
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.
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 activex
n
, 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.
Axes | Figures |
© 1994-2005 The MathWorks, Inc.