In the previous video, you learned how to handle different ranges of values using if and if-else statements. Those statements are useful for one or two cases, but they can become unwieldy when you have more than three possibilities. In this video, learn how to use the switch statement to handle multiple cases.
- [Instructor] In the previous movie, I showed you how to handle different ranges of values using if...and, if...else statements. Those statements are useful for one or two cases, but they can quickly become unwieldy when you have more than three possibilities. In this movie, I will show you how to use the switch statement to handle multiple cases. I have a blank command window in front of me, but what I really need to do is create a script. So on the "Home" tab, I will go over to the "New Script" button and click it, and that gives me a new editor.
I could also have pressed control N to create a new script. I want to define a function, so I will type that keyword "function". The return variable that I will return out of the function, will be "spc", and I'm setting that variable equal to the result of the function, which I will call "spcode", and you'll see why in a second. I don't want to pass in any arguments, so I will type a blank left and right parentheses.
I'll press enter twice to give myself some space, and then I can start writing my code. The first will be the input value that I want, which I'm doing from inside the function. So I will call that "val1", and that will be equal to some user input. So equal to, "input", left parentheses, and then the prompt, which will be "Enter the number "of the center", and that'll be followed by a colon, then a backslash, and an "n", that's the newline character.
That will cause the user to write their input on a new line, instead of next to the prompt that I gave. And then a right parentheses, and, so that we don't have the input value echoed, I'll type a semicolon. The scenario I have in mind, is that a user is looking up the code that was used to examine a specimen. So we're looking for a lab code. We'll type in a number, and we'll get a city back.
In this case, the company has four labs, so I will press enter, do that twice, and then type "switch". The switch keyword allows me to define a number of cases. And the variable that we'll be looking at is "val1", which came via our input statement. So I'll press enter, and first we need to define our first case, so I'll type "case", C-A-S-E, space, and then the number 14, then enter, and if the number that's entered through input is 14, then "spc", which is our return value, will be equal to the string in single quotes, of "Mt. Crawford".
That's M-T period C-R-A-W-F-O-R-D, then enter. And then next, we'll do "case", and you can see that MATLAB is handling the indentation for me. 15, enter, and that'll be "spc" equals the string "Portland", close the string. Then "case" for 16. And in that case, "spc" will be equal to "Syracuse", close quote, good. And then the last enumerated case will be case 17, and in that case "spc" will be equal to "Calgary".
And in case you're wondering, yes, these are all places that I've lived. It's how writers have fun when we create courses like this. All right. With that out of the way, then we need to know how to handle any value that is not 14 through 17. So enter, and then, my favorite keyword in this course, which is "otherwise". Other languages use case...else, or "else", in this case, but I believe that "otherwise" is absolutely brilliant. It's a great choice. All right, so I'll press enter, and if we don't have 14, 15, 16, or 17, then our return variable "spc" will be equal to the following string, "Not a recognized lab code".
And close out the string, and press enter. Now we need to close out the switch. So that'll be "end", press enter, and now we need to end the function, so I'll type "end" again, and everything matches up. And I'll press control s to save, and I'll call it "spcode", so I'll just leave it as it is. And click "Save". Now that I have my code, I can close my editor window. So I'll click the close editor button, and go back to the command window.
Now I can run "spcode" just by typing in the name of the function. "spcode", enter. Enter the number of the center, so I'll call it 16, and enter, and we get Syracuse. If I enter in the number 10, so I'll run "spcode" again, enter the number 10, and we get "Not a recognized lab code." And it looks like I could have used semicolons for the assignment so that the value would not be echoed. But as you can see, the switch statement that I created allows me to handle cases, and if I get a case that it doesn't recognize, it tells me.
- Defining variables and contains
- Exploring operators
- Summarizing with built-in functions
- Generating random numbers
- Defining vectors and matrices
- Accepting input in scripts
- Writing and reading data from external files
- Creating custom functions
- Using conditional logic
- Repeating operations with loops
- Working with text strings
- Plotting data and function output
- Formatting, saving, and printing plots
- Using statistical and matrix functions