Dialog Boxes Applications of Cell Arrays
description
Transcript of Dialog Boxes Applications of Cell Arrays
1. Reminder of Symbols2. Dialog Boxes3. Output-centric dialogs4. Input-centric dialogs
Dialog BoxesApplications of Cell Arrays
1
1. Reminders on Symbols
• Creating/hard-coding: Braces { }• Referencing content: Braces { }
• Augmenting: Brackets [ ]
• Referencing container: Parentheses ()
2
2. Dialog Boxes
3
2. Dialog Boxes
• Dialog boxes are "popup windows" that allows us another means to communicate with the user.
• Some dialog boxes to collect input:inputdlg(), listdlg(), menu(), questdlg()
• And some to produce output:msgbox(), helpdlg(), warndlg(), errordlg()
4
5
Output-centric Dialog Boxes
Output-centric dialog boxes are popup windows with the primary purpose of providing information to the user.
Examples are: msgbox(), helpdlg(), warndlg(), and errordlg()
6
Output-centric Dialog Boxes
Output-centric dialogs are "non-blocking" – they do NOT (on their own) prevent the rest of the program from executing. They can, however be created as "modal". A modal dialog prevents the user from interacting with other windows until it is closed but does not interfere with the execution of the program.
7
Output-centric Dialog Boxes
Output-centric dialogs are "non-blocking" – they do NOT (on their own) prevent the rest of the program from executing. They can, however be created as "modal". A modal dialog prevents the user from interacting with other windows until it is closed but does not interfere with the execution of the program.
You may wish to create a blocking output-centric box. This is accomplished by collecting the return value of the dialog box function call and then using either the waitfor() or uiwait() function:
wh = msgbox('Here is some information.', 'Special Info!');waitfor(wh);
8
Output-centric Dialog Boxes
Output-centric dialogs are "non-blocking" – they do NOT (on their own) prevent the rest of the program from executing. They can, however be created as "modal". A modal dialog prevents the user from interacting with other windows until it is closed but does not interfere with the execution of the program.
You may wish to create a blocking output-centric box. This is accomplished by collecting the return value of the dialog box function call and then using either the waitfor() or uiwait() function:
wh = msgbox('Here is some information.', 'Special Info!');waitfor(wh);
uiwait() provides a programmatic as well as a user-based means to continue. Use uiresume() to continue.
msgbox(), helpdlg(), warndlg(), errordlg()
Each creates a dialog box that will output a string
% calculate/displaytravelTime = distance/speeds(type);resultString = sprintf('With this plane, it will
take %.2fhrs.\n', travelTime);msgbox(resultString)
9
msgbox()
• Use sprintf() customize the data in the message box– Creates a formatted string
%calculate/displaytravelTime = distance/speeds(type);resultString = sprintf('With a %s, it will take
%.2fhrs.\n', myPlanes{type}, travelTime);msgbox(resultString)
10
msgbox()
• Use sprintf() customize the data in the message box– Creates a formatted string
%calculate/displaytravelTime = distance/speeds(type);resultString = sprintf('With a %s, it will take
%.2fhrs.\n', myPlanes{type}, travelTime);wh = msgbox(resultString);waitfor(wh);
11
12
Input-centric Dialog Boxes
Input-centric dialog boxes are those boxes which are intended to have the user provide information.
Input-centric dialogs are "blocking" – which is sometimes incorrectly referred to as "modal".
A modal dialog prevents the user from interacting with other windows until it is closed. A blocking dialog prevents the rest of the program from executing until it is closed.
3. inputdlg()
• Collects information from the user• inputdlg() – much like the input() function, except…
– It is presented in a pop-up window form– There can be multiple prompts and multiple values provided by the
user.
• ALL user-provided information is returned as strings in a cell array!
14
inputdlg(), syntax
• Arguments– A cell array of strings: This is the set of prompts for the user. For
example:prompts={'Prompt 1', 'Prompt 2', 'Prompt N'}
• Return Values– One (1) cell array of strings: What the user provided in the boxes
• Exampleprompts={'Coeff "a":', 'Coeff "b":', 'Coeff "c":'};coeffs = inputdlg(prompts);
var = inputdlg(prompts);
cell array, indicated by the curly braces.
inputdlg(), output
15
• When collecting numbers, use str2double() to convert the cell array into a numerical vector:
coeffs = str2double(coeffs);
4. listdlg()
• listdlg() – Create and open list-selection dialog box– User does not have to type anything: Prevent typing errors!
16
This function returns 2 values.
17
[Selection, ok] = listdlg(<param name>, <param value>)
listdlg()calling-syntax
• Selection is a vector of indices of the selected strings
• Ok is a recognition of user selection• 1 if user clicks the OK button• 0 if clicks click the Cancel button or
close the dialog box (X)
18
[Selection, ok] = listdlg(<param name>, <param value>)
listdlg()calling-syntax
There are many parameters available to the listdlg() function, so the calling syntax is a bit different.
19
[Selection, ok] = listdlg(<param name>, <param value>)
listdlg() calling-syntax
There are many parameters available to the listdlg() function, so the calling syntax is a bit different.
20
[Selection, ok] = listdlg(<param name>, <param value>)
listdlg()calling-syntax
There are many parameters available to the listdlg() function, so the calling syntax is a bit different.
Instead of relying on the order of the arguments, the programmer provides both the name of a parameter and the argument value for that parameter.
In all cases, the 'ListString' parameter must be provided. This parameter is a cell array of strings containing the options that will be provided to the user.
21
[Selection, ok] = listdlg('ListString', S)
listdlg()- syntax
• Arguments are in parameter/value pairs– Parameter goes 1st, value of the parameter goes 2nd.
• 'ListString' – Cell array of strings that specify list
S = {'Item 1', 'Item 2', 'Item N'}
myList is a cell array of strings: { }
Why cell arrays?The length of each selection varies widely, and
a regular array would not be rectangular.
Sample use of listdlg()
22
23
S = {'My choice', 'Your choice', 'His choice', 'Her choice'}[Selection, ok] = listdlg('ListString', S)
listdlg() calling syntax
'SelectionMode' - 'single' or 'multiple''Name' – Title string'ListSize' – Vector of width and height in pixels'PromptString' – String or cell array'OKString' – String for OK button'CancelString' – String for Cancel button
[Selection, ok] = listdlg('ListString', S, …'SelectionMode', 'single', 'ListSize', [160, 160])
listdlg() - Question
• If the user selects Primary Booster and Secondary Booster, What will Selection - the first return value - be after this executes?
a. {'Primary Booster','Secondary Boosters'}b. [1 3]c. {1, 3}d. None of the above
24
Experiment in the command window!
• What button did the user hit?
a. The 'ok' buttonb. The 'cancel' buttonc. The X that closes the windowd. Either b or ce. None of the above
25
26
[Selection, ok] = listdlg('ListString', S, …'SelectionMode', 'Single')
listdlg()- syntax
• SelectionMode - A second argument parameter/value pair – Allows user to select a Single or Multiple (default) items
2nd PAIR of arguments
27
The Select All button is gone.
2nd PAIR of arguments
28
• What item did the user select?
Example: Aircraft Time
• Create a program that estimates the time an aircraft takes to travel a certain distance.
Aircrafts possible, with their average speeds are:1) Cessna 150, 198 kmph2) Boeing 787, 950 kmph3) Concorde, 2147 kmph4) Cessna 421, 444 kmph
29
Algorithm
% prompt user for type of airplane (error?)% prompt user for distance to travel (error?)
% calculate/display
• Presented is the evolution from:– Option1: use input() and if. – Option2: use input() and vectors. – Option3: using listdlg(), vectors and cell arrays.
30
Option #1. input(), if and while
%prompt user for type of airplanetype = input('Enter the type of airplane: \n1 – cessna 150\n
2-Boeing 787\n3-Concorde\n4-Cessna 421\n Enter now: ');
%prompt user for distance to traveldistance = input('Enter the distance (km): ');
%calculate/displayif type == 1 %cessna 150
travelTime = distance/198;fprintf('With this plane, it will take %.2fhrs.\n', travelTime);
elseif….
31
Add while loops to trap errors.
Option #2. input(), vectors, while
%prompt user for type of airplanetype = input('Enter the type of airplane: \n1 – cessna 150\n
2-Boeing 787\n3-Concorde\n4-Cessna 421\n Enter now: ');
%prompt user for distance to traveldistance = input('Enter the distance (km): ');
%data base of speedsspeeds = [198, 950, 2147, 444];
%calculate/displaytravelTime = distance/speeds(type);fprintf('With this plane, it will take %.2fhrs.\n',
travelTime);
32
Add while loops to trap errors.
Reference the correct value in the vector, using the index.
Option #3. listdlg(), arrays, while
%prompt user for type of airplanemyPlanes = {'Cessna 150', 'Boeing 787', 'Concorde', 'Cessna
421'};type = listdlg('ListString', myPlanes,'SelectionMode',
'Single');
%prompt user for distance to traveldistance = inputdlg('Enter the distance (km): ');
%data base of speedsspeeds = [198, 950, 2147, 444];
%calculate/displaytravelTime = distance/speeds(type);fprintf('With this plane, it will take %.2fhrs.\n', travelTime);
33
Add while loop to trap errors, and convert to number
Option #3. Sample
• Note: once a program starts with dialog boxes, it should use only dialog boxes…
>> not the command window
34
Menus
• menu()– Provides the user with a vertical list of buttons to select (No limit)– Returns the index of the button selected– How do we work with this?
35
Options = {'Cheese', 'Water', 'Cake', 'Pie', 'Oranges'};Choice = menu('Here are the dessert options. What would you like?', Options)
Menus
• questdlg()– Offers the user a maximum of three push buttons to select options– Returns the actual string used in the selected button
• menu()– Provides the user with a vertical list of buttons to select (No limit)– Returns the index of the button selected
36
37
File and Directory Selection
To have the user choose a file:filename = uigetfile(FilterSpec)
Where FilterSpec is a: - column vector cell array of file extension patterns
{'*.txt'; '*.dat'; '*.csv'}
fname = uigetfile({'*.dat';'*.txt'; '*.csv'})
38
File and Directory Selection
To have the user choose a file:filename = uigetfile(FilterSpec)
Where FilterSpec is a: - two-column matrix cell array of file extension patterns and descriptive text
{ '*.txt', 'Text files (.txt)'; '*.dat', 'Data files (.dat), '*.csv', 'Comma-separated files (.csv)' }
fname = uigetfile({'*.dat', 'Data files (.dat)'; '*.txt', 'Text files (.txt)'; '*.csv', 'Comma-separated (.csv)'})
39
File and Directory Selection
To have the user choose a directory (folder):folderPath = uigetdir(startPath)
Where startPath is a string with the entire path to the folder, and folderPath is a string with the entire path to the selected folder.
folder = uigetdir('c:\windows\system32')
(MATLAB on Windows can use forward slashes as folder separators)
NOTE: The special path designators '.' and '..' can also be used in the path:'.' The current directory'..' The parent directory
40
More Information
Mathworks: Pre-defined Dialog Boxes
http://tiny.egr115.com/?id=47
http://www.mathworks.com/help/matlab/predefined-dialog-boxes.html