Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

Share this Page URL
Help

Chapter 3. Selection Statements > The is Functions in MATLAB - Pg. 98

98 Chapter 3 selection statements pickpizzaifelse.m %This script asks the user for a type of pizza % and prints which type to order using if-else mypick = menu(`Pick a pizza',`Cheese',`Shroom',`Sausage'); if mypick == 1 disp(`Order a cheese pizza') elseif mypick == 2 disp(`Order a mushroom pizza') elseif mypick == 3 disp(`Order a sausage pizza') else disp(`No pizza for us today') end praCtICe 3.5 Write a function that will receive one number as an input argument. It will use the menu function to display `Choose a function' and will have buttons labeled `fix', `floor', and `abs'. Using a switch statement, the function will then calculate and return the requested function (e.g., if `abs' is chosen, the function will return the absolute value of the input argument). Choose a fourth function to return if the user does not push a button, as described earlier. 3.7 the IS FunCtIons In MatlaB There are a lot of functions that are built into MATLAB that test whether or not something is true; these func- tion names begin with the word is. Since these func- tions are frequently used in if statements, they are introduced in this chapter. For example, the function called isletter returns logical 1 if the argument is a let- ter of the alphabet, or 0 if it is not: >> isletter(`h') ans = 1 >> isletter(`4') ans = 0 This result could also be found by comparing a character's position within the character encoding, for example, for lower case letters. the programming Concept mychar >= `a' && mychar <= `z' the efficient Method isletter(mychar)