Lecture+10+Notes (2)

Lecture+10+Notes (2) - 1 14:440:127 Introduction to...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
1 14:440:127– Introduction to Computers for Engineers Notes for Lecture 10 Rutgers University, Fall 2010 Instructor- Brenda V. Cortez 1 Graphical User Interfaces Sometimes we want the user to be able to manipulate options interactively with the mouse and keyboard, rather than through the command line, and in MatLab we can facilitate this by creating a Graphical User Interface . These GUIs consist of a Figure window containing menus, buttons, text, graphics, etc. . One example of MatLab’s GUIs is the already implemented menu function, which will be examplained in the first part below. However, you can also create your own GUIs to provide particular functionality to the user. The second part of these Lecture Notes will explain the fundamentals of this process. 2 Menu The menu function is often used in conjuction with a selection structure, such as the switch/case and/or if/elseif structure. This function causes a menu box to appear on the screen, with a series of buttons defined by the programmer - you. input = menu(‘Message to user’,‘button 1 text’,‘button 2 text’,etc.); 2.1 Menu - Spring Break Destinations You can use a menu when you want to ensure that the user chooses only from the options provided for which you have information(statements to execute). This also means that you will NOT need the otherwise syntax, since it is not possible to pick an option that is NOT on the menu . disp(‘Spring Break Destinations’) disp(‘-------------------------’) city = menu(‘Select a city from the menu:’, . .. ‘Boston’,‘Denver’,‘Honolulu’); switch city case 1 disp(‘Flight will be $245’) case 2 disp(‘Flight will be $195’) case 3 disp(‘Stay Home and study. ..’) end In the past you have seen this same problem, but one in which the case numbers were strings ,
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
2 as opposed to numbers in each case line above. When the script is executed, a menu box will appear and wait for the user to select one of the buttons, and proceed to execute the statements for the button picked. 2.2 Menu - Gas Cost Calculation Following is another example using the menu function but using the if/else structure instead of the switch/case structure. % cost per gallon rate = 2.89; % Ask the user to input gallons or liters, using a menu disp(‘Use the menu box to make your selection’) choice = menu(‘Measure the gasoline in liters or gallons?’,. .. ‘gallons’,‘liters’); if(choice==1) factor = 1; unit = ‘gallons’; else factor = 0.264; unit = ‘liters’; end % Ask the user how much gas he/she would like to buy % The [] are used to concatenate the 3 strings into one and also % because the input function will ONLY take 1 or 2 inputs volume = input([‘Enter the volume you’d like to buy in ’,unit,‘\n’]); % Calculate the cost $ of the gas cost = volume*factor*rate; % Send the result to the screen fprintf(‘That will be %5.2f for %0.2f %s. \n’,cost,volume,unit); Notice that in this if/else example, you also do NOT need the else
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/24/2011 for the course ENGINEERIN 127 taught by Professor Finch during the Spring '08 term at Rutgers.

Page1 / 13

Lecture+10+Notes (2) - 1 14:440:127 Introduction to...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online