Open the Block Parameters dialog of the Switch Case block and enter as follows:.Place a Switch Case block in the current system and attach the input port labeled u1 to the source of the data you are evaluating.You construct a Simulink switch control flow statement like the example shown as follows: The preceding switch control flow statement can be represented by the following pseudocode: If a case value (in brackets) corresponds to the actual value of the input, its Switch Case Action subsystem is executed. The cases are evaluated top down starting with the top case. Each output port case condition is attached to a Switch Case Action subsystem. The following shows a completed Simulink C-like switch control flow statement in the subsystem of the Switch Case block.Ī Switch Case block receives a single input, which it uses to form case conditions that determine which the subsystem to execute. Implement a C-like switch control flow statement Thank you.Switch Case (Simulink Reference) Simulink Reference 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. 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |