dialog boxes applications of cell-arrays

41
1. Reminder of Symbols 2. Dialog Boxes 3. listdlg() 4. msgbox() 5. questdlg() 6. menu() Dialog Boxes Applications of Cell-Arrays 1

Upload: blaise

Post on 14-Feb-2016

46 views

Category:

Documents


0 download

DESCRIPTION

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 (). - PowerPoint PPT Presentation

TRANSCRIPT

Page 4: Dialog Boxes Applications of Cell-Arrays

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!

44

Page 7: Dialog Boxes Applications of Cell-Arrays

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

7

Page 9: Dialog Boxes Applications of Cell-Arrays

listdlg() return-values What will the first return

value be after this executes?

a. ['Primary Booster','Secondary Boosters']

b. [1 3]c. {1, 3}d. None of the above

9

Remember: you choose the name of variables used to collect the return values.

Page 10: Dialog Boxes Applications of Cell-Arrays

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

10

Page 11: Dialog Boxes Applications of Cell-Arrays

[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!

11

Page 22: Dialog Boxes Applications of Cell-Arrays

3.a. Full Example 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 kph2) Boeing 787, 950 kph3) Concorde, 2147 kph4) Cessna 421, 444 kph

22

Page 23: Dialog Boxes Applications of Cell-Arrays

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.

23

Page 24: Dialog Boxes Applications of Cell-Arrays

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

24

Add while loops to trap errors.

Page 25: Dialog Boxes Applications of Cell-Arrays

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

Add while loops to trap errors.

Reference the correct value in the vector, using the index.

Page 26: Dialog Boxes Applications of Cell-Arrays

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

Add while loop to trap errors, and convert to number

Reference the correct value in the vector, using the index.

Page 30: Dialog Boxes Applications of Cell-Arrays

4. msgbox() A little improvement:

%calculate/displaytravelTime = distance/speeds(type);resultString = sprintf('With a %s, it will take

%.2fhrs.\n', ??????, travelTime);msgbox(resultString)

30

Task: Replace "this plane" by the actual name!

Page 31: Dialog Boxes Applications of Cell-Arrays

4. msgbox()%prompt user for type of airplanemyPlanes = {'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};

31REFERENCE the CONTENT, using curly braces.

Page 33: Dialog Boxes Applications of Cell-Arrays

Make the program error proof!%prompt user for type of airplanemyPlanes = {'Cessna 150', 'Boeing 787', 'Concorde',

'Cessna 421'};[type ok] = listdlg('ListString',

myPlanes,'selectionmode', 'single');

%if user hits ok, continueif ok==1

%prompt user for distance to traveldistance = inputdlg('Enter the distance (km): ');%code as before

else%user hit cancel of closed box..%do other stuff

end 33

Page 34: Dialog Boxes Applications of Cell-Arrays

Done with that example.

Note how much our programs have improved since week 2, and yet not too many lines of code were required.

Hopefully, you're pausing and coding this at home.

Use the tools in the final project if you want. Make sure to error-proof accordingly.

34

Page 35: Dialog Boxes Applications of Cell-Arrays

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.

35

Page 37: Dialog Boxes Applications of Cell-Arrays

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)

37

Page 41: Dialog Boxes Applications of Cell-Arrays

Wrapping Up Dialog boxes are user friendly but require a little more

coding If you’re writing a “quick and dirty” program, dialogs may

be overkill – consider the command window instead. We saw:

inputdlg() listdlg() msgbox() questdlg() menu()

It’s difficult to memorize syntax. Practice it and use MATLAB Help to remind yourself quickly!

41