CS 211 Lesson 25
Dialog Boxes
Quote:
Pain is temporary. Quitting is forever. Lance Armstrong
Lesson Objectives:
Lesson:
I. MATLAB Concepts
A. Dialog boxes
- Dialog boxes are "Graphical User Interface" (GUI) elements that allow a program to communicate with a user.
- Some dialog boxes display information to a user.
- Some dialog boxes allow the user to provide input to a program.
B. Modal vs. non-modal dialog boxes
- Modal dialog boxes require a user response before any other window in the application can be accessed.
- Modal dialog boxes should only be used for warnings or error messages that need urgent attention.
- Modal dialog boxes restrict the options a user has for a particular moment in time.
- Modal dialog boxes are easier to program (because the state of the application is known when the user responds).
- Non-modal dialog boxes do not block access to other application windows.
- Non-modal dialog boxes give more options to a user.
- Non-modal dialog boxes require more error checking in your program because the state of the application may have changed when the user eventually uses the dialog box.
C. Commonly used dialog boxes
MATLAB function
Description
Example Code and Resulting Dialog Box
msgbox display a message to the user;
by default, the dialog box is non-modalmsgbox('message','title')
msgbox('I like ice cream', 'FYI')
errordlg
displays an error message;
user must click ok to continueerrordlg('errorstring','dlgname')
errordlg('Invalid file name', 'File Error')
warndlg
displays a warning message;
user must click ok to continuewarndlg('warningstring','dlgname')
warndlg('Division by zero is undefined', 'Warning')
helpdlg
displays a help message;
user must click ok to continue
(always non-modal)helpdlg('helpstring','dlgname')
helpdlg('Choose 3 pts from the figure', 'Point Selection')
inputdlg
prompts for one or more values and returns the corresponding user input
answer = inputdlg(prompt,dlgname,num_lines,defAns)
Coords = inputdlg({'x=', 'y='}, 'Enter coordinates:', 1, {'0','0'})
listdlg
allows a user to select one or more items from a list (always modal)
[Selection,ok] = listdlg('ListString',S,...)All arguments come in 'PropertyName', 'PropertyValue' pairs. See MATLAB's help system for all possible properties.[Selection OK] = listdlg('Name', 'Pick a color:', 'ListString', {'green', 'yellow', 'red'}, 'ListSize', [150, 50])
questdlg
asks a question with 2 or 3 possible answers (by default: yes, no, and cancel)
button = questdlg('qstring','title')
Answer = questdlg('Did you vote?', 'Vote Survey')
uigetfile
displays an OS's file selection dialog box; returns the selected file's name and directory path
[file_name folder] = uigetfile('FilterSpec','DialogTitle')
[Name Folder] = uigetfile('*.dat', 'Select input data file:')
uigetdir
displays an OS's directory selection dialog box; returns the selected file's directory path
folder_name = uigetdir('start_path','dialog_title')
Folder_name = uigetdir('C:\Documents and Settings', 'Select the desired folder')
uiputfile
displays an OS's file selection dialog box; warns if the user selects an existing file
[file_name folder] = uiputfile('FilterSpec','DialogTitle')
[Name Folder] = uiputfile('*.dat', 'Save file:');
printdlg
displays a printer dialog box (optionally in setup mode) for printing a figure
printdlg(figure)
plot(-pi:0.1:pi, cos(-pi:0.1:pi));
printdlg(figure(1))
or
printdlg('-setup', figure(1))
- Details concerning the arguments for the functions listed above can be found in the MATLAB on-line help system. Most of the dialog box functions have several optional arguments that can be used to modify the functionality of the dialog box.
II. Good Programming Practices
Whenever possible, make dialog boxes non-modal because it
gives more freedom to the program's user.
Only "pop up" an error or warning message in a dialog box if
the user can do something about the problem the dialog box describes.
Consider the user's perspective when creating titles and informative text for dialog boxes -- not the programmer's perspective. Make the titles and informative text in dialog boxes as un-ambiguous as possible.
III. Algorithms
(none for this lesson)
Lab Work: Lab 25
References: Chapman Textbook: section 10.6