MATLAB can implement common control flow operations such as switch statements. Learn how to use a switch statement to choose between several user inputs.
- [Instructor] Another control statement we can put to use is the switch statement. Switch statements are great for cleaning up nested if statements if they start to get out of control or for implementing patterns like state machines. So let's see how we set this up in MATLAB. Our myFirstScript is getting a little out of control here. So I'm going to just create a new file, new script. And let's call it LetsSwitch. I'll close my first script. Let's clear our workspace, and clear screen. Open up LetsSwitch and let's see how to put this together.
I'm going to add in a comment here, say switch statement, and for our switch statement, let's get two values from the user as inputs and then switch on the type of calculation they want to perform, whether that's add, subtract, multiply, or divide. So for our first number, I'll just call it n here, and we'll grab an input. Make sure to use single quotes for our input, and let's type in, enter a number.
Looks good. Let's go ahead and copy that and paste it to get our other number, call that one m. And we'll say Enter another number. Oop, looks like I misspelled number there. And let's ask the user what calculation they want to perform. I'll make a variable called action, and we'll grab some more input. And let's ask what calculation should I perform.
And so this action's what will switch on. So let's create our switch statement. We'll switch, and we see MATLAB recognizes it by turning blue, and we're going to switch on the action. So let's assume that our user enters a string that's either add, subtract, multiply or divide. So for our first case, we'll handle add. Again in MATLAB, we don't have to take care of organizing things with curly braces, it's all handled with indentation. So let's add in output variable, and we'll do n+m.
I'm not going to put a semicolon here so that we can see our output on the console. I'm going to go ahead and copy our output statement here, 'cause we'll reuse that. And let's type in our next case. Again, we see MATLAB handling the indentation for us. And let's handle the subtract case here. Paste and subtract. For our next case, we'll multiply, paste and multiply.
And for our next case, we'll divide. Paste and divide. Now in switch statements we have to provide some sort of default case that occurs if none of the other cases are matched. So in some languages that's the default case but in MATLAB it's the otherwise case. So we type otherwise, and so if our user says anything other than add subtract multiply or divide, we'll just give them a message. We'll display and I'll say, Sorry Dave, I can't do that.
And we'll wrap up our switch with an end statement. Let's go ahead and save this file, and let's run it. So we see in our console here, we've got LetsSwtich. Let's enter a number. We'll enter six. And then it asks for another number, we'll enter five. And what calculation should I perform? So let's add 'em. I'll give a string, make sure to put it in the single quotes here. And we succeed, we've got 11. So just for fun, let's test and see if we can get into our otherwise case. So I'm going to press the up arrow till I get back to our LetsSwitch.
Here we go. And let's enter a number. Give us a little more real estate here. Let's enter a number. Eight. Enter another one. 10. And what calculation should I perform? I want to perform the flip calculation. So I tell my state machine to flip, it says Sorry Dave, I can't do that.
- Creating MATLAB variables
- Working with matrix and scalar operations
- Using if statements and loops
- Creating functions
- Importing data
- Building basic plots and 3D plots
- Working with images
- Creating Simulink models