Docsity

Introduction to Graphical User Interface (GUI) in MATLAB 6.5, Lecture notes of Design

Partial preview of the text

Download Introduction to Graphical User Interface (GUI) in MATLAB 6.5 and more Lecture notes Design in PDF only on Docsity!

UAE UNIVERSITY

COLLEGE OF ENGINEERING

ELECTRICAL ENGINEERING DEPARTMENT

IEEE UAEU STUDENT BRANCH

Introduction to Graphical User Interface (GUI)

MATLAB 6.

Presented By:

Refaat Yousef Al Ashi 199901469 Ahmed Al Ameri 199900378

Coordinated By:

Prof. Abdulla Ismail Abdulla

Introduction

A graphical user interface (GUI) is a pictorial interface to a program. A good GUI can make programs easier to use by providing them with a consistent appearance and with intuitive controls like pushbuttons, list boxes, sliders, menus, and so forth. The GUI should behave in an understandable and predictable manner, so that a user knows what to expect when he or she performs an action. For example, when a mouse click occurs on a pushbutton, the GUI should initiate the action described on the label of the button. This chapter introduces the basic elements of the MATLAB GUIs. The chapter does not contain a complete description of components or GUI features, but it does provide the basics required to create functional GUIs for your programs.

1.1 How a Graphical User Interface Works

A graphical user interface provides the user with a familiar environment in which to work. This environment contains pushbuttons, toggle buttons, lists, menus, text boxes, and so forth, all of which are already familiar to the user, so that he or she can concentrate on using the application rather than on the mechanics involved in doing things. However, GUIs are harder for the programmer because a GUI-based program must be prepared for mouse clicks (or possibly keyboard input) for any GUI element at any time. Such inputs are known as events, and a program that responds to events is said to be event driven. The three principal elements required to create a MATLAB Graphical User Interface are

  1. Components. Each item on a MATLAB GUI (pushbuttons, labels, edit boxes, etc.) is a graphical component. The types of components include graphical controls (pushbuttons, edit boxes, lists, sliders, etc.), static elements (frames and text strings), menus, and axes. Graphical controls and static elements are created by the function uicontrol, and menus are created by the functions uimenu and uicontextmenu. Axes, which are used to display graphical data, are created by the function axes.
  2. Figures. The components of a GUI must be arranged within a figure, which is a window on the computer screen. In the past, figures have been created automatically whenever we have plotted data. However, empty figures can be created with the function figure and can be used to hold any combination of components.
  3. Callbacks. Finally, there must be some way to perform an action if a user clicks a mouse on a button or types information on a keyboard. A mouse click or a key press is an event, and the MATLAB program must respond to each event if the program is to perform its function. For example, if a user clicks on a button, that event must cause the MATLAB code that implements the function of the button to be executed. The code executed in response to an event is known as a call back. There must be a callback to implement the function of each graphical component on the GUI. The basic GUI elements are summarized in Table 1.1, and sample elements are shown in Figure 1.1. We will be studying examples of these elements and then build working GUIs from them.

Table 1.1 Some Basic GUI Components

Figure 1.1 A Figure Window showing examples of MA TLAB GUI elements. From top to bottom and left to right, the elements are: (1) a pushbutton; (2) a toggle button in the 'on' state; (3) two radio buttons surrounded by a frame; (4) a check box; (5) a text field and an edit box; (6) a slider; (7) a set of axes; and (8) a list box.

  1. Use a MATLAB tool called guide (GUI Development Environment) to layout the Components on a figure. The size of the figure and the alignment and spacing of components on the figure can be adjusted using the tools built into guide.
  2. Use a MATLAB tool called the Property Inspector (built into guide) to give each component a name (a "tag") and to set the characteristics of each component, such as its color, the text it displays, and so on.
  3. Save the figure to a file. When the figure is saved, two files will be created on disk with the same name but different extents. The fig file contains the actual GUI that you have created, and the M-file contains the code to load the figure and skeleton call backs for each GUI element.
  4. Write code to implement the behavior associated with each callback function.

Step 2: To layout the components on the GUI, run the MATLAB function guide. When guide is executed, it creates the window shown in Figure 1.2.

Figure 1.4 The completed GUI layout within the guide window

First, we must set the size of the layout area, which will become the size the final GUI. We do this by dragging the small square on the lower right corner of the layout area until it has the desired size and shape. Then, click on the "pushbutton" button in the list of GUI components, and create the shape of the pushbutton in the layout area. Finally, click on the "text" button in the list GUI components, and create the shape of the text field in the layout area. The resulting figure after these steps is shown in Figure 1.4. We could now adjust the alignment of these two elements using the Alignment Tool, if desired.

Step 3: To set the properties of the pushbutton, click on the button in the layout area and then select "Property Inspector" from the toolbar. Alternatively, right-click on the button and select "Inspect Properties" from the popup menu. The Property Inspector window shown in Figure 1.5 will appear. Note this window lists every property available for the pushbutton and allows us set each value using a GUI interface. The Property Inspector performs the same function as the get and set functions, but in a much more convenient form.

Figure 1.5 The Property Inspector showing the properties of the pushbutton. Note that the String is set to 'Click Here', and the Tag is set to 'MyFirstButton'.

For the pushbutton, we may set many properties such as color, size, font, text alignment, and so on. However, we must set two properties: the String property, which contains the text to be displayed, and the Tag property, which is the name of the pushbutton. In this case, the String property will be set to 'click Here', and the Tag property will be set to MyFirstButton. For the text field, we must set two properties: the String property, which contains the text to be displayed, and the Tag property, which is the name of the text field. This name will be needed by the callback function to locate and update the text field. In this case, the String property will be set to 'Total clicks: 0', and the Tag property defaulted to 'MyFirstText'. The layout area after these steps is shown in Figure 1.6. It is possible to set the properties of the figure itself by clicking on a clear spot in the Layout Editor, and then using the Property Inspector to examine and set the figure's properties. Although not required, it is a good idea to set the figure's Name property. The string in the Name property will be displayed in the title bar of the resulting GUI when it is executed.

Figure 1.7 Typing MyFirstGUI in the Command Window starts the GUI.

MyFirstGUI.fig. If function MyFirstGUI is called with arguments, then the function assumes that the first arguments the name of a sub function, and it calls that function using feval, passing the other arguments on to that function. Each callback function handles events from a single GUI component. If a mouse click (or keyboard input for Edit Fields) occurs on the GUI component, then the component's callback function will be automatically called by MATLAB. The name of the callback function will be the value in the Tag property of the GUI component plus the characters "_Callback". Thus, the callback function for MyFirstButton will be named MyFirstButton_Callback. M-files created by guide contain callbacks for each active GUI component, but these callbacks simply display a message saying that the function of the callback has not been implemented yet.

Step 5: Now, we need to implement the callback sub function for the pushbutton. This function will include a persistent variable that can be used to count the number of clicks that have occurred. When a click occurs on the pushbutton, MATLAB will call the function MyFirstGUI with MyFirstButton_callback as the first argument. Then function MyFirstGUI will call sub function MyFirstButton_callback, as shownin Figure 1.9. This function should increase the count of clicks by one, create a new text string containing the count, and store the new string in the String property of the text field MyFirstText.

Figure 1.8 The M-file forMyFirstGUI, automatically created by guide.

A function to perform this step is shown below:

MyFirstButton_Callback. This function increments count, and then saves the new count in the text field on the GUI.

Figure 1.10 The resulting program after three button pushes.

Note that this function declares a persistent variable count and initializes it to zero. Each time that the function is called, it increments count by 1 and creates a new string containing the count. Then, the function updates the string displayed in the text field MyFirstText. The resulting program is executed by typing MyFirstGUI in the Command Window. When the user clicks on the button, MATLAB automatically calls function MyFirstGUI with MYFirstButton_Callback as the first argument, and function MyFirstGUI calls sub function MyFirstButton_Callback. This function increments variable count by one and updates the value displayed in the text field. The resulting GUI after three button pushes is shown in Figure 1.10.

Good Programming Practice

Use guide to layout a new GUI, and use the Property Inspector to set the initial properties of each component such as the text displayed on the component, the color of the component, and the name of the callback function, if required. After creating a GUI with guide, manually edit the resulting function to add comments describing its purpose and components and to implement the function of callbacks.

1.2.1 A Look Under the Hood

Figure 1.8 shows the M-file that was automatically generated by guide for MyFirstGUI. We will now examine this M-file more closely to understand how it works.

First, let's look at the function declaration itself. Note that this function uses varargin to represent its input arguments and varargout to represent its output results. Function varargin can represent an arbitrary number of input arguments, and function varargout can represent a varying number of output arguments. Therefore, a user can call function MyFirstGUI with any number of arguments.

Calling the M-File without Arguments

If the user calls MyFirstGUI without arguments, the value returned by margin will be zero. In this case, the program loads the Gm from the figure file MyFirstGUI.fig using the openfig function. The form of this function is

fig = openfig(mfilename, ‘reuse’);

where mfilename is the name of the figure file to load. The second argument in the function specifies whether there can be only one copy of the figure running at a given time, or whether multiple copies can be run. If the argument is 'reuse', then only one copy of the figure can be run. If openfig is called with the 'reuse' option and the specified figure already exists, the preexisting figure will be brought to the top of the screen and reused. In contrast, if the argument is 'new', multiple copies of the figure can be run. If openfig is called with the' new' option, a new copy of the figure will be created each time. By default, a GUI created by guide has the' reuse' option, so only one copy of the figure can exist at any time. If you want to have multiple copies of the GUI, you must manually edit this function call. After the figure is loaded, the function executes the statement

This function sets the background color of the figure to match the default background color used by the computer on which MATLAB is executing. This function makes the color of the GUI match the color of native windows on the computer. Therefore, a GUI can be written on a Windows-based PC and used on a UNIX-based computer, and vice versa. It will look natural in either environment. The next two statements create a structure containing the handles of all the objects in the current figure and store that structure as application data in the figure.

Function guihandles creates a structure containing handles to all of the objects within the specified figure. The element names in the structure correspond to the Tag properties of each GUI component, and the values are the handles of each component. For example, the handle structure returned in MyFirstGUI.m is

There are three GUI components in this figure the figure itself, plus a text field and a pushbutton. Function guidata saves the handles structure as application data in the figure, using the setappdata function. The final set of statements here returns the figure handle to the caller if an output argument was specified in the call to MyFirstGUI.

To add local data to the handles structure, we must manually modify the Mfile after it is created by guide. A programmer adds the application data to the handles structure after the call to guihandles and before the call to guidata. For example, to add the number of mouse clicks count to the handles structure, we would modify the program as follows:

This application data will now be passed with the handles structure to every callback function, where it can be used. A version of the pushbutton callback function that uses the count value in the handles data structure is shown below. Note that we must save the handles structure with a call to guidata if any of the information in it has been modified.

Good Programming Practice

Store GUI application data in the handles structure, so that it will automatically be available to any callback function.

If you modify any of the GUI application data in the handles structure, be sure to save the structure with a call to guidata before exiting the function where the modifications occurred.

1.2.4 A Few Useful Functions

Three special functions are used occasionally in the design of callback functions: gcbo, gcbf, and findobj. Although these functions are less needed with MATLAB 6.5 GUIs than with earlier versions, they are still very useful, and a programmer is sure to encounter them. Function gcbo (get callbackobject) returns the handle of the object that generated the callback, and function gebf (get callbackfigure) returns the handle of the figure containing that object. These functions can be used by the callback function to determine the object and figure producing the callback, so that it can modify objects on that figure.

Function findobj searches through all of the child objects within a parent object, looking for ones that have a specific value of a specified property. It returns a handle to any objects with the matching characteristics. The most common form of findobj is

where parent is the handle of a parent object such as a figure, 'Property' is the property to examine, and 'Value' is the value to look for. For example, suppose that a programmer would like to change the text on a pushbutton with the tag' Buttonl ' when a callback function executes. The programmer could find the pushbutton and replace the text with the following statements

1.3 Object Properties

Every GUI object includes an extensive list of properties that can be used to customize the object. These properties are slightly different for each type of object (figures, axes,uicontrols,etc.).All of the properties for all types of objects are documented on the online Help Browser, but a few of the more important properties for figure and uicontrol objects are summarized in Tables 1.2 and 1.3. Object properties may be modified using either the Property Inspector or the get and set functions. Although the Property Inspector is a convenient way to adjust properties during GUI design, we must use get and set to adjust them dynamically from within a program, such as in a callback function.

Table 1.2 Important figure Properties

1.4.1 Text Fields A text-field is a graphical object that displays a text string. You can specify how the text is aligned in the display area by setting the horizontal alignment property. By default, text fields are horizontally centered. A text field is created by creating a uicontrol whose style property is 'edit'. A text field may be added to a GUI by using the text tool in the Layout Editor. Text fields do not create callbacks, but the value displayed in the text field can be updated in a callback function by changing the text field's String property, as shown in Section 1.2.

1.4.2 Edit Boxes An edit box is a graphical object that allows a user to enter a text string. The edit box generates a callback when the user presses the Enter key after typing a string into the box. An edit box is created by creating a uicontrol whose style property is 'edit'. An edit box may be added to a GUI by using the edit box tool in the Layout Editor. Figure l.l1a shows a simple GUI containing an edit box named, ‘Edit Box’ and a text field named 'TextBox' .When a user types a string into the edit box, it automatically calls the function EditBox_Callback, which is shown in Figure 1.11b. This function locates the edit box using the handles structure and recovers the string typed by the user. Then, it

locates the text field and displays the string in the text field. Figure 1.12 shows this GUI just after it has started and after the user has typed the word "Hello" in the edit box.

1.4.3 Frames A frame is a graphical object that displays a rectangle on the GUI. You can use frames to draw boxes around groups of logically related objects. For example, a frame is used to group the radio buttons together on Figure 1.1. A frame is created by creating a uicontrol whose style property is 'frame'. A frame maybe added to a GUI by using the frame tool in the LayoutEditor. Frames do not generate callbacks.

1.4.4 Pushbuttons A pushbutton is a component that a user can click on to trigger a specific action. The pushbutton generates a callback when the user clicks the mouse on it. A pushbutton is created by creating a uicontrol whose style property is 'pushbutton'. A pushbutton may be added to a GUI by using the pushbutton tool in the Layout Editor. Function MyFirstGUI in Figure 1.10 illustrated the use of pushbuttons.

Figure 1.11 (a) Layout of a simple GUI with an edit box and a text field. (b) The callback functions for this GUI.