270 likes | 382 Views
Dialog Boxes Applications of Cell-Arrays. Reminder of Symbols Dialog Boxes Inputdlg () listdlg () msgbox (). 1. Reminders on Symbols. Creating/hard-coding: Braces { } Referencing to content: Braces { } Augmenting: Brackets [ ] Referencing to container: Parentheses ().
E N D
Dialog BoxesApplications of Cell-Arrays Reminder of Symbols Dialog Boxes Inputdlg() listdlg() msgbox()
1. Reminders on Symbols • Creating/hard-coding: Braces { } • Referencing to content: Braces { } • Augmenting: Brackets [ ] • Referencing to 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(), warndlg() 99% of the time, the command deals with cell arrays, either as arguments, as return-values, or both! 4
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! 5
inputdlg(), syntax coeffs = 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 cell-array (cargo-ship) into a numericalvector: coeffs = str2double(coeffs); 7
4. listdlg() • listdlg() – Create and open list-selection dialog box • User does not have to type anything
listdlg()-syntax [Selection, ok] = listdlg(‘ListString’, S) This functionreturns 2 return-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() - Question • If the user selects Primary Booster and Secondary Booster, What will the Selection return-value be after this executes? • {'Primary Booster','Secondary Boosters'} • [1 3] • {1, 3} • None of the above
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 string: { } Why cell arrays? The length of each selection varies widely, and an regular-array would not be rectangular.
Experiment in the command window! • What button did the user hit? • The 'ok' button • The 'cancel' button • The [x] that 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 software 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. (week 2,3.4) • Option2: use input() and vectors. (week 10) • 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 Reference the correct value in the vector, using the index.
Option #3. Sample • Note: once a software starts with dialog boxes, it should end with dialog boxes… >> not in the command window..
5. msgbox() • Creates a dialog box that will output either a number or a string to the user (not both) %calculate/display travelTime = distance/speeds(type); resultString = sprintf('With this plane, it will take %.2fhrs.\n', travelTime); msgbox(resultString)
4. 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)
Examples: Florida Energy Usage • Using a spreadsheet of energy usage in Florida: • Request the user to enter a date and energy source using dialog boxes • Determine the year of the highest production of energy.
On your own: Explore the following dialog boxes • questdlg() • Offers the user an 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
Key Ideas • Prompts are typically stored in Cell Arrays • Dialog boxes: • inputdlg() • Returns a cell array of user inputs • listdlg() • Returns index of selection and confirmation of selection • msgbox() • Use sprintf() to format the string used for output • Dialog boxes are user friendly • A full GUI is just as effective • Require a lot of clicking vs. entering data in the command window.