Chapter 7: Graphical User Interface

Introduction

Graphical User Interfaces, or GUIs, are tools that improve how the user can interact with a code by modifying the appearances of inputs, messages, or other notices. As a result, users can type inputs or interact with codes through pop-up windows instead of using the command window. The following section will discuss various GUI commands, such as menu, input dialog, and message box.

GUI is a powerful way to improve the usability of a code. By using a GUI, it is easier for users to input and read data and removes the need for users to interact directly with the command window. This capability makes it easier for users to input data and much easier to write codes which allow users to select from several options.

The list dialog Command (listdlg)

The listdlg command enables the user to select from several predetermined options from the user to select from several predetermined choices for inputs.  This command replaces the command “menu“, which is no longer recommended for creating lists to select from in MATLAB. The menu function may still be present in codes more than a few years old and is useful to understand. If using menu,  the following format creates a prompt accompanied by buttons with each listed option within the command. An example of this with an accompanying output is shown below.

>> a = menu('What option would you like?','First','Second','Third');

The listdlg syntax is similar to the older menu command. An example of the listdlg is shown below that outputs the same menu as the above example. First, a variable is created which is assigned an array of each list entry that will be in the menu. After the list is defined, the format of the list and the name of the list variable are entered into the listdlg function.

 

list = {'First','Second','Third'};
a = listdlg('ListString',list);
Adding options to the basic “ListString” selection enables further customization of the command. The option “PromptString” allows an instruction to be printed on the output as well. Additionally, the “SelectionMode” function allows for customization of the number of options that can be selected in the list. These features are shown added to the ongoing example below.
list = {'First','Second','Third'};

a = listdlg('PromptString',{'Select an Option:'}, 'SelectionMode','single','ListString',list);

This code would generate the following textbox within MATLAB. Once the user selects one of the options, the variable a will be renamed to correspond with the selected option. Note that the variable will be named based on the order of the selected option, and not based on the text placed in each box. For this example, if the user selects the first box labeled “First”, the variable a will be assigned the value a=1. This enables the author of the code to place any text within the box and not have it affect what the variable is named.

 

The Input Dialog (inputdlg) Command

The input dialog function is like the menu command in the fact that it enables the user to interact with their codes and can interact with their codes through a pop-up window. Instead of selecting from a button as in the menu command, the input dialog command enables the user to input data or values into their code in a textbox. The following example shows how to format the input dialog command. The format for this command is slightly more complex than the menu command due to its increased features. The first portion of the code contained in brackets and quotations and highlighted in yellow is the message which will be displayed in the textbox. The second portion highlighted in blue shows the message which can be displayed at the top of the textbox. The green portion shows how many characters as tall and long the textbox will be. In most applications, a relatively small box with dimensions around 1×20 characters is enough. The second example of the inputdlg function depicts how to input two values into the function. Note that each message is separated by a comma.

x = inputdlg ({'What is the smallest value you wish to test?'},'value', [1 20]);

The Message Box (msgbox) Command

The msgbox function creates a message box that can display a message in a pop-up or can modify the fprintf function to display a message in a pop-up. An example of a msgbox function that displays a message and an image is shown below. Visit the Mathworks msgbox webpage to view a more complete listing of images available for the msgbox function. Within the msgbox function, the text highlighted in yellow is the main message in the box. The text highlighted in green in the second set of parentheses is the title which appears next to the MATLAB logo in the top header of the pop-up. The third piece of data highlighted in blue is the code for the image which appeared in the msgbox.

f = msgbox('Invalid Value', 'Error','error');

Examples

The following code is a unit converter developed by an Iowa State faculty member. This code can be modified to include a msgbox function to increase the code’s usability, as shown in the second version. This example also demonstrates various elements of MATLAB addressed thus far, such as while loops, if statements, and nested loops.

License

Icon for the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License

A Guide to MATLAB for ME 160 Copyright © 2022 by Austin Bray and Reza Montazami is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License, except where otherwise noted.