In today’s lesson, we’re going to cover the following topics:
1. Switchcase
2. Menus
video coming soon
Switchcase
Switchcase statements are those that will compare variables to specific values, and if the condition matches then the code within that condition will be executed. This is one of the 4 types of control flow logic (recall the 4 types – if-else, switchcase, while loops and for loops).
This is usually only used where we are comparing whether a variable (either numbers or strings) is equal to another value or not – not with greater than or less than comparisons. This is the syntax:
Here is an explanation of each line within it. The syntax that is required to even make the switchcase statements turns blue in MATLAB. The words in black are the ones you can change.
(a) switch variable_to_compare
Replace variable_to_compare with the variable you want to compare values of, in order for your code to execute.
(b) case set_variable_equal_to_a_value
Replace set_variable_equal_to_a_value with the variable you mentioned above as the variable_to_compare and set it equal to a value, either words (a string) or numbers.
(c) code_to_execute
This is the code you want to execute when each variable is equal to its respective expression in part (b).
Let’s do an example to see how this works. Say you want to create a code that will print a statement to display the number a user enters from 1 to 4.
1) The first step to thinking through this problem would be understanding that you will need some way to get a user to enter numbers into a script, because you can’t define the numbers to be compared in advance. To do this, we will create an input statement that will ask the user to enter a number, and I will set this input statement equal to the variable a. You can set it equal to any variable, but you should do this because we will need to use variable a later on in the switchcase statement. Click here is you missed the lesson on input statements.
2) Now we will need to be able to take the number that a user enters and compare them to either being equal to 1, 2, 3, 4, or another value – this type of comparison can be done using switchcase. The first line of a switchcase statement has the words switch a. You have to use whatever variable you want the switchcase to be comparing, and for us this is variable a which is the number the user had entered with the input statement.
3) Next to each case we need to specify the different scenarios – either a=1, a=2, a=3, a=4 or it does not equal any of those values, in which case we should specify to the user that the number they input does not match with any of the numbers we want to display statements for. We will use a new case statement for each line, but we don’t need to repeat the variable a next to each case. Rather than writing a=1, a=2, etc, we will just put the numbers 1, 2, 3, or 4 right next to the word case in each line.
To incorporate any remaining scenarios you don’t want to separately specify, you can make a new case if there are only a few other cases you want to define, or use an otherwise block to cover all other possible scenarios. Using the otherwise block is not necessary, and we can replace it with another case instead, but it does get tedious if you want to classify each scenario separately. This question only asks that we display values for 1, 2, 3, and 4, so let’s assume that all other values can just be displayed as “this value is not in the list.”
4) Lastly, we will need MATLAB to display the numbers depending on what was entered. This will replace the words code_to_execute in the switchcase statements. To display the words, I will use disp. Click here if you missed the lesson on how to print values in MATLAB.
I have numbered the code so you can refer to the steps I explained above and see what part of the code I am explaining:
Now we can hit the Run button, and this is what comes up in the Command Window:
Type in a number when the prompt comes up. I have typed in 10, and MATLAB displays the words “This value is not in the list.” This is because it recognizes that the number 10 is not in any of the cases we described in the cases above, so it falls under the otherwise block.
When I run the code again and use the number 2, MATLAB recognizes this was one of the cases and displays the words “The value is 2.”
Let’s do another example, where we want to check whether a student is in our project group or not. Say the names of the 3 students in our group include Bob, Ken, and Sally. The user should enter a name, and if that name is in this group, display “The student is in the group.” Otherwise, display “This student is not in the group.”
Here are the steps to thinking through this problem:
1) First we need the user to enter a name – this time it will not be a number, but it will be a string because it will be treated as text. This will need an ‘s’ within the input brackets to indicate it is a string. This is explained in the Input Statements lesson – click here if you missed it.
2) We will use switchcase to be able to compare the user’s input with the names of the students in the group. This means the switch and end statements, with the cases and otherwise statement in there, as explained in the example above. When creating each case, names should be in single quotes (you will see how the names Bob, Sally and Ken are in purple) because they are strings.
3) We will display the words “This student is in the group” or “This student is not in the group” after MATLAB compares the name entered by using switchcase. Here is what it would look like putting it all together.
Running the code brings this up. I was prompted to enter a name, and I entered Bobby. MATLAB recognized that this name was not in the group, and it executed the words under the otherwise block from the switchcase in the code from the image above.
Now when I enter the name Sally, it will display that the student is in the group, because case Sally is one of the cases because that student is in the group.
Menus
We can create pop-up menus in MATLAB that let the user press a button and interact with it. This can usually replace using an input statement with strings, so the user isn’t typing in the answer but rather selecting it from a menu. Let’s do an example where you will ask the user for their favorite color. The choices are red, green, blue, or other. This is a simple one line of code:
Let’s break down this line of code:
(a) Choice on the left side of the equals sign is the name I will assign this menu. Setting the menu equal to a variable will be useful when you want to call the menu and do specific actions if the user presses choice 1, choice 2, etc. We will get into this later. However, you can call the variable anything you want!
(b) menu: The word menu is the built-in menu function, so you will have to keep this the same and then use curved brackets right after menu to input all your choices and the question you want to ask the user.
(c) ‘Whats your favorite color?’: This is the first part within the brackets, and this is where the question you want to ask the user will have to be. Make sure you have single quotes around the question, and you use a comma after it to start adding in the possible choices.
(d) ‘Red’,’Blue’,’Green’,’Other’: These are the possible choices I want to display on my menu. You should be able to add as many choices as you want, but I have chosen 4 options, specifically colors for the answer to my question.
When you run the code, this is what pops up:
The user should then be able to press any button on the menu. But … what will happen if the user presses each button? Nothing! We didn’t define anything for each button. A good structure to use for defining functionality for your menu is switchcase, which we learned above.
Let’s do an example: We want the sentence “Apples are red” to appear if the user presses Red. We want “Blueberries are blue” to appear if the user presses Blue. We want “Kiwis are green” to appear if the user presses Green. Lastly, we want “The world is a rainbow” to appear if the user presses Other. Let’s utilize what we learned with switchcase above to create this control flow.
Check out the screenshot below. The first line needs to be the word switch followed by the variable that stores the user input. In this case, the user will select something from the menu, so the variable choice stores that value. This is why I mentioned above that it is important to set your menu equal to a variable. This is why switch choice is on Line 2. I have added in the end statement as well, since we can’t forget to include that either! Now all the remaining code will go between Lines 2 and 3:
I have now added in the outline of switch case. Recall that we use the word case for each different case when using switchcase. The cases in this problem will be the choices on the menu – there are 4 of them, so we need case 1, case 2, case 3, and case 4. You don’t have to specify anything else with the case names, since MATLAB knows that case 1 refers to the first menu option, case 2 refers to the second, and so on. I have also added the otherwise statement (which is optional) but I won’t put anything there – because we don’t have any other options (aside from the 4) for the user to select, there is nothing that will go under otherwise.
Now adding in the sentences that we need to display for each case. I will use a simple disp statement for this. Recall that in order to print a sentence, we can use disp, followed by brackets with the words you want to display in single quotes.
Let’s run the code now! This menu pops up first:
I will select red from here, and this is what the Command Window shows:
This is correct – we wanted to display “Apples are red” if the user pressed red!