1 / 36

Dialog Boxes Applications of Cell-Arrays

Dialog Boxes Applications of Cell-Arrays. Reminder of Symbols Dialog Boxes listdlg() msgbox() questdlg() menu(). 1. Reminders on Symbols. Creating/hard-coding: Braces { } Referencing to content: Braces { } Augmenting: Brackets [ ] Referencing to container: Parentheses ().

eman
Télécharger la présentation

Dialog Boxes Applications of Cell-Arrays

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Dialog BoxesApplications of Cell-Arrays Reminder of Symbols Dialog Boxes listdlg() msgbox() questdlg() menu()

  2. 1. Reminders on Symbols • Creating/hard-coding: Braces { } • Referencing to content: Braces { } • Augmenting: Brackets [ ] • Referencing to container: Parentheses () Most likely to be used!

  3. 2. Dialog Boxes 3

  4. 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

  5. 3. listdlg() • listdlg() – Create and open list-selection dialog box • Major Advantage: User does not have to type anything • Less spelling errors!

  6. >>doc listdlg() <enter> This function returns 2 return-values.

  7. [Selection,ok] = listdlg('ListString',S) Selection is a vector of indices of the selected strings (in single selection mode, its length is 1).Selection is [] when ok is 0. ok is 1 if you click the OK button, or 0 if you click the Cancel button or close the dialog box. Double-clicking on an item or pressing Return when multiple items are selected has the same effect as clicking the OK button. The dialog box has a Select all button (when in multiple selection mode) that enables you to select all list items.

  8. listdlg() return-values • What will the first return-value be after this executes? • {'Primary Booster','Secondary Boosters'} • [1 3] • {1, 3} • None of the above

  9. [Selection,ok] = listdlg('ListString',S) Selection is a vector of indices of the selected strings (in single selection mode, its length is 1).Selection is [] when ok is 0. ok is 1 if you click the OK button, or 0 if you click the Cancel button or close the dialog box. Double-clicking on an item or pressing Return when multiple items are selected has the same effect as clicking the OK button. The dialog box has a Select all button (when in multiple selection mode) that enables you to select all list items. • If user hits cancel or closes the dialog box, an empty-vector is returned, AND ok is set equal to 0. • This means the second return-value can be used to see what the user did!

  10. What did user do? • Did user hit ok? • Did user hit cancel? • Either way: • Had s/he selected anything anyway? • If user did click ok, then ok will be true, which equals 1. • If user didnot click ok, then ok will be false, which equals 0.

  11. [Selection,ok] = listdlg('ListString',S) Inputs are in parameter/value pairs (i.e. they go 2 by 2): Parameter goes 1st, value of the parameter goes 2nd. The actual string:

  12. Experiment in the command window! 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.

  13. 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

  14. A second parameter Though additional arguments are not necessary, you may choose to add them AS A PAIR. For example:

  15. 2nd PAIR of inputs. The Select All button is gone.

  16. 2nd PAIR of inputs. • What did the user select? • It cannot be determined • __________________________

  17. 3.a. Full Example • 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

  18. 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.

  19. #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.

  20. #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.

  21. #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.

  22. #3. Output • Note: once a software starts with dialog boxes, it should end with dialog boxes… >> not in the command window.. 

  23. 4. msgbox() • A little improvement: %calculate/display travelTime = distance/speeds(type); resultString = sprintf('With this plane, it will take %.2fhrs.\n', travelTime); msgbox(resultString)

  24. 4. msgbox() • A little improvement: %calculate/display travelTime = distance/speeds(type); resultString = sprintf('With a %s, it will take %.2fhrs.\n', ??????, travelTime); msgbox(resultString) Task: Replace "this plane" by the actual name!

  25. 4. msgbox() %prompt user for type of airplane myPlanes = {'Cessna 150', 'Boeing 787', 'Concorde', 'Cessna 421'}; type = listdlg('ListString', myPlanes,'selectionmode', 'single'); • Remember: this is the index (i.e. location) of the string selected. • This is the cell-array of all the names. • To reference the name selected using the index selected: planeSelected = myPlanes{type}; REFERENCE the CONTENT, using curly braces.

  26. 4. msgbox() • A little improvement: %calculate/display travelTime = distance/speeds(type); resultString = sprintf('With a %s, it will take %.2fhrs.\n', myPlanes{type}, travelTime); msgbox(resultString)

  27. Make the software error proof! %prompt user for type of airplane myPlanes = {'Cessna 150', 'Boeing 787', 'Concorde', 'Cessna 421'}; [type ok] = listdlg('ListString', myPlanes,'selectionmode', 'single'); %if user hits ok, continue if ok==1 %prompt user for distance to travel distance = inputdlg('Enter the distance (km): '); %code as before else %user hit cancel of closed box.. %do other stuff end

  28. Done with that example. • Note how much a software has improved since your first knowledge of week2, and yet not too many lines of code were required. • Hopefully, you're pausing and coding this in parallel. • Use them in the final project if you want. Make sure to error-proof accordingly.

  29. 5. questdlg() • Typical call: button = questdlg('qstring','title','str1','str2', 'str3','default') • qstring = Question to ask the user • title = Title for dialog box • str1 = String to show on Button #1 • str2 = String to show on Button #2 • str3 = String to show on Button #3 • default = String that is the default button • button = string on the button that was clicked Caution: Maximum of 3 buttons.

  30. 5. Quick experiment • button = string on the button that was clicked

  31. 6. menu() – vertical menu • Typical call: button = menu('qstring','bt1','bt2',……,'btn') • qstring= question to ask user • bt1 = String to show on Button #1 • bt2 = String to show on Button #2 • Can have as many options as desired. • There is no default answer. • Return value: Button number clicked (not the string)

  32. 6. menu(), with cell-arrays

  33. 6. Output

  34. What's next??? • Yes, we are done with cell-arrays and dialog boxes.. So what's next? Files!

  35. Wrapping Up • Dialog boxes are user friendly but time taking • Dialog boxes should not be used when the software is not meant to be sold. Lots of clicking vs. entering data in the command window. Choose carefully. • We saw: • inputdlg() • listdlg() • msgbox() • questdlg() • menu() • NEVER learn the syntax by heart. Practice it enough, then use the doc to remind yourself quickly!

More Related