Presentation is loading. Please wait.

Presentation is loading. Please wait.

1.Reminder of Symbols 2.Dialog Boxes 3.Output-centric dialogs 4.Input-centric dialogs Dialog Boxes Applications of Cell Arrays 1.

Similar presentations


Presentation on theme: "1.Reminder of Symbols 2.Dialog Boxes 3.Output-centric dialogs 4.Input-centric dialogs Dialog Boxes Applications of Cell Arrays 1."— Presentation transcript:

1 1.Reminder of Symbols 2.Dialog Boxes 3.Output-centric dialogs 4.Input-centric dialogs Dialog Boxes Applications of Cell Arrays 1

2 1. Reminders on Symbols Creating/hard-coding: Braces { } Referencing content:Braces { } Augmenting:Brackets [ ] Referencing container: Parentheses () 2

3 2. Dialog Boxes 3

4 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() 5

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

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); 7

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

9 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) 9

10 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) 10

11 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); 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. 12

13 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 Example prompts={'Coeff "a":', 'Coeff "b":', 'Coeff "c":'}; coeffs = inputdlg(prompts); 14 var = inputdlg(prompts); cell array, indicated by the curly braces.

15 inputdlg(), output 15 When collecting numbers, use str2double() to convert the cell array into a numerical vector: coeffs = str2double(coeffs);

16 4. listdlg() listdlg() – Create and open list-selection dialog box – User does not have to type anything: Prevent typing errors! 16

17 This function returns 2 values. 17 [Selection, ok] = listdlg(, ) 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 18 [Selection, ok] = listdlg(, ) listdlg() calling-syntax There are many parameters available to the listdlg() function, so the calling syntax is a bit different.

19 19 [Selection, ok] = listdlg(, ) listdlg() calling-syntax There are many parameters available to the listdlg() function, so the calling syntax is a bit different.

20 20 [Selection, ok] = listdlg(, ) 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 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'}

22 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 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])

24 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

25 Experiment in the command window! What button did the user hit? a.The 'ok' button b.The 'cancel' button c.The X that closes the window d.Either b or c e.None of the above 25

26 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

27 2nd PAIR of arguments 27 The Select All button is gone.

28 2nd PAIR of arguments 28 What item did the user select?

29 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 kmph 2)Boeing 787, 950 kmph 3)Concorde,2147 kmph 4)Cessna 421,444 kmph 29

30 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

31 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…. 31 Add while loops to trap errors.

32 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); 32 Add while loops to trap errors. Reference the correct value in the vector, using the index.

33 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); 33 Add while loop to trap errors, and convert to number

34 Option #3. Sample Note: once a program starts with dialog boxes, it should use only dialog boxes… >> not the command window 34

35 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)

36 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'} 37 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)' } 38 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 39

40 More Information Mathworks: Pre-defined Dialog Boxes http://tiny.egr115.com/?id=47 http://www.mathworks.com/help/matlab/predefined-dialog-boxes.html 40


Download ppt "1.Reminder of Symbols 2.Dialog Boxes 3.Output-centric dialogs 4.Input-centric dialogs Dialog Boxes Applications of Cell Arrays 1."

Similar presentations


Ads by Google