
This should show you why using a switch case statement can be cleaner than its equivalent in an if-then-else statement. And we can see that works also, that either alpha or bravo would work just fine there. And then something specific if it's Charlie. Now, maybe we're interested in doing one thing if it's alpha or bravo. And then we're going to switch through that railroad switch yard based on the different cases. Let's take a look here with strings where we can just say, my string is equal to alpha. One of the other advantages of switch case in where it's most often actually used is with strings. If we come here and put our value as 8 and run it again, we can see that it just goes through and says something else.

That's how the switch case works.Īnd if we run it here, we can see that it indeed did find the correct value. And if none of those were correct, then we're just going to say otherwise and do something else. Otherwise, we're going to try case two, case three. If the case is 1, that is if val equals 1, display 1. And so we're going to be switching based on the variable val. Basically, what we're doing is saying, I want to switch through these cases as if it was a switch yard at a train depot or something like that. Notice there's just a lot less characters on screen when we do it this way. How would this look if it were done as a switch? I think this is more clean and easy to understand doing it this way. Notice in this if/else statement, what we have is someone is creating a val equal to 3 variable, and then checking with a bunch of if/elses to find out what the value is. To force an early return from a function, place a return statement in the function at the point where you want to exit.Today in MATLAB Basics, we're going to discuss the use of switch case statements as a cleaner, more maintainable way of doing something like this, that you often see done with else/if statements. Statement2 % Attempt to recover from errorĭisp 'Operation failed' % Handle the error You can use this to attempt to recover from an error caught in the first try section: You can also nest try/ catch blocks, as shown here. If(strfind(errmsg, 'Inner matrix dimensions'))ĭisp('** Wrong dimensions for matrix multiply')įor more information: See Checking for Errors with try-catch in the MATLAB "Programming and Data Types" documentation You are switching on a character vector, but your cases are all logical. In this case, the catch statements check the error message that was issued (returned by lasterr) and respond appropriately. Mitch, the cases that you use in switch need to be a compatible datatype with the switch expression. If a statement in the try segment of the block fails, control passes to the catch segment. The example below shows a try/ catch block within a function that multiplies two matrices.
#Matlab switch case code
When you have statements in your code that could possibly generate unwanted results, put those statements into a try/ catch block that will catch any errors and handle them appropriately. In these examples, you get an error when choice equals 2, because x is undefined. The same holds true for if/ ifelse statements. Since MATLAB executes only one case of any switch statement, variables defined within one case are not known in the other cases of that switch statement. In this example, if result is 52, only the first disp statement executes, even though the second is also a valid match: Using break within a case statement is not only unnecessary, it is also invalid and generates a warning.

In MATLAB, case statements do not fall through only one case may execute. In C, if you don't end each case with a break statement, code execution falls through to the following case. The first case below tests for either a linear or bilinear method by using a cell array in the case statement. You can test against more than one condition with switch. You need strcmp to compare strings of different lengthsĪ useful difference between switch/ case statements in MATLAB and C is that you can specify string values in MATLAB case statements, which you cannot do in C. It is possible, but usually not advantageous, to implement switch/ case statements using if/ elseif instead. Control passes to the caller of the function. Immediately exits the function in which it appears. Control passes to next iteration of the same loop. Skips any remaining statements in the current loop. In nested loops, control passes to the next outer loop. Make sure you use these functions appropriately.Įxits the loop in which it appears. It's easy to confuse the break, continue, and return functions as they are similar in some ways.


Multiple Conditions in a case Statement.This section covers the following topics: MATLAB Programming Tips (Programming and Data Types) Programming and Data Types
