420 likes | 693 Views
Dialog Boxes Applications of Cell Arrays. Reminder of Symbols Dialog Boxes Output-centric dialogs Input-centric dialogs. 1. Reminders on Symbols. Creating/hard-coding: Braces { } Referencing content: Braces { } Augmenting: Brackets [ ] Referencing container : Parentheses ().
E N D
Dialog BoxesApplications of Cell Arrays Reminder of Symbols Dialog Boxes Output-centric dialogs Input-centric dialogs
1. Reminders on Symbols • Creating/hard-coding: Braces { } • Referencing content: Braces { } • Augmenting: Brackets [ ] • Referencing container: Parentheses ()
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()
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()
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 closedbut does not interfere with the execution of the program.
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 closedbut 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);
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 closedbut 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/display travelTime = distance/speeds(type); resultString = sprintf('With this plane, it will take %.2fhrs.\n', travelTime); msgbox(resultString)
msgbox() • Use sprintf() customize the data in the message box • Creates a formatted string %calculate/display travelTime = distance/speeds(type); resultString = sprintf('With a %s, it will take %.2fhrs.\n', myPlanes{type}, travelTime); msgbox(resultString)
msgbox() • Use sprintf() customize the data in the message box • Creates a formatted string %calculate/display travelTime = distance/speeds(type); resultString = sprintf('With a %s, it will take %.2fhrs.\n', myPlanes{type}, travelTime); wh = msgbox(resultString); waitfor(wh);
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 stringsin a cell array!
inputdlg(), syntax var = inputdlg(prompts); • 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 • Example prompts={'Coeff "a":', 'Coeff "b":', 'Coeff "c":'}; coeffs = inputdlg(prompts); cell array, indicated by the curly braces.
inputdlg(), output • Whencollectingnumbers, use str2double() to convert the cellarrayinto a numericalvector: coeffs = str2double(coeffs); 15
4. listdlg() • listdlg() – Create and open list-selection dialog box • User does not have to type anything: Prevent typing errors!
listdlg()calling-syntax [Selection, ok] = listdlg(<param name>, <param value>) This function returns 2 values. • Selection is a vector of indices of the selected strings • Ok is a recognition of user selection • 1if user clicks the OK button • 0if clicks click the Cancel button or close the dialog box (X)
listdlg()calling-syntax [Selection, ok] = listdlg(<param name>, <param value>) There are many parameters available to the listdlg() function, so the calling syntax is a bit different.
listdlg() calling-syntax [Selection, ok] = listdlg(<param name>, <param value>) There are many parameters available to the listdlg() function, so the calling syntax is a bit different.
listdlg()calling-syntax [Selection, ok] = listdlg(<param name>, <param value>) 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.
listdlg()- syntax [Selection, ok] = listdlg('ListString', S) • Arguments are in parameter/valuepairs • Parameter goes 1st, value of the parameter goes 2nd. • 'ListString' – Cell array of strings that specify list S = {'Item 1', 'Item 2', 'Item N'}
Sample use of listdlg() 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.
listdlg() calling syntax S = {'My choice', 'Your choice', 'His choice', 'Her choice'} [Selection, ok] = listdlg('ListString', S) '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? • {'Primary Booster','Secondary Boosters'} • [1 3] • {1, 3} • None of the above
Experiment in the command window! • What button did the user hit? • The 'ok' button • The 'cancel' button • The Xthat closes the window • Either b or c • None of the above
listdlg()- syntax [Selection, ok] = listdlg('ListString', S, … 'SelectionMode', 'Single') • SelectionMode - A second argument parameter/valuepair • Allows user to select a SingleorMultiple(default) items
2nd PAIR of arguments The Select All button is gone.
2nd PAIR of arguments • 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: • Cessna 150, 198 kmph • Boeing 787, 950 kmph • Concorde, 2147 kmph • Cessna 421, 444 kmph
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.
Option #1. input(), if and while %prompt user for type of airplane type = 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 travel distance = input('Enter the distance (km): '); %calculate/display if type == 1 %cessna 150 travelTime = distance/198; fprintf('With this plane, it will take %.2fhrs.\n', travelTime); elseif…. Add while loops to trap errors.
Option #2. input(), vectors, while %prompt user for type of airplane type = 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 travel distance = input('Enter the distance (km): '); %data base of speeds speeds = [198, 950, 2147, 444]; %calculate/display travelTime = distance/speeds(type); fprintf('With this plane, it will take %.2fhrs.\n', travelTime); 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 airplane myPlanes = {'Cessna 150', 'Boeing 787', 'Concorde', 'Cessna 421'}; type = listdlg('ListString', myPlanes,'SelectionMode', 'Single'); %prompt user for distance to travel distance = inputdlg('Enter the distance (km): '); %data base of speeds speeds = [198, 950, 2147, 444]; %calculate/display travelTime = distance/speeds(type); fprintf('With this plane, it will take %.2fhrs.\n', travelTime); Add while loop to trap errors, and convert to number
Option #3. Sample • Note: once a programstarts with dialog boxes, it should use only dialog boxes… >> not the command window
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? 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
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'})
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)'})
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
More Information Mathworks: Pre-defined Dialog Boxes http://tiny.egr115.com/?id=47 http://www.mathworks.com/help/matlab/predefined-dialog-boxes.html