How to Creating and Displaying a Graphical User Interface in MATLAB?

Creating and Displaying a Graphical User Interface:–

MATLAB GUIs are created using a tool called guide, the GUI Development Environment. This tool allows a programmer to layout the GUI, selecting and aligning the GUI components to be placed in it. Once the components are in place, the programmer can edit their properties: name, color, size, font, text to display, and so forth. When guide saves the GUI, it creates a working program including skeleton functions that the programmer can modify to implement the behavior of the GUI. When a guide is executed, it creates the Layout Editor, shown in Figure 1.2. The large white area with grid lines is the layout area, where a programmer can layout the GUI. The Layout Editor window has a palate of GUI components along the left side of the layout area. A user can create any number of GUI components by first clicking on the desired component, and then dragging its outline in the layout area. The top of the window has a toolbar with a series of useful tools that allow the user to distribute and align GUI components, modify

the properties of GUI components, add menus to GUIs, and so on. The basic steps

required to create a MATLAB GUI are:

1. Decide what elements are required for the GUI and what the function of each element will be. Make a rough layout of the components by hand on a piece of paper.

GUI  WINDOW:–

 

 

As an example of these steps, let’s consider a simple GUI that contains a single

pushbutton and a single text string. Each time that the pushbutton is clicked, the text string will be updated to show the total number of clicks since the GUI started.

Figure 1.2

 

 

 

 

Graphical User Interface Components

 

 

This section summarizes the basic characteristics of common graphical user interface components. It describes how to create and use each component, as well as the types of events each component can generate. The components discussed in this section are:-

 

 

  • • Text Fields

 

  • • Edit Boxes

 

  • • Frames

 

  • • Pushbuttons

 

  • • Toggle Buttons

 

  • • Checkboxes

 

  • • Radio Buttons

 

  • • Popup Menus

 

  • • List Boxes

 

  • • Slide

 

 

 

 

 

The process of GUI require following steps:-

  • Initializing GUIDE (GUI Creator)
  • Creating the Visual Aspect of the GUI
  • Writing the Code for the GUI Callbacks
  • Launching the GUI

 

Initializing GUI (guide creator):-

First, open up MATLAB. Go to the command window and type in a guide.

 

You should see the following screen appear. Choose the first option

Blank GUI (Default):-

 

 

 

GUI window appears:-

 

 

 

 

 

 

 

 

 

 

 

 

Creating a visual effect in GUI:-

 

Add in all these components to the GUI by clicking on the icon and placing it onto the grid. At this point, your GUI should look similar to the figure below:

 

 

 

 

 

 

 

Writing the code for the GUI call back:-

MATLAB automatically generates a .m file to go along with the figure that you just put together. The .m file is where we attach the appropriate code to the callback of each component. For the purposes of this tutorial, we are primarily concerned only with the callback functions. You don’t have to worry about any of the other function types.

Open up the .m file that was automatically generated when you saved your GUI. In the MATLAB editor, click on the icon, which will bring up a list of the functions within the .m file. Select CALLBACK FOR EACH.

 

Launching the GUI:-

The first way is through the GUIDE editor. Simply press the icon on the GUIDE editor.

 

 

How to Work on Graphical User Interface (GUI) in MATLAB?

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 UI control, and menus are created by the functions UI menu and UI context menu. 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 keypress 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 callback. 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.