1 / 14

Know how to use switch case in matlab with examples

Get to know how to use switch case it mat ab. Watch this presentation till the end to explore the best ways to use switch case in matlab.

Download Presentation

Know how to use switch case in matlab with examples

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Know How to Use Switch Case in Matlab With Examples • Presented by: Matlabassignmenthelp

  2. The full form of Matlab is Matrix Laboratory, which one of the best coding languages that are used for technical and scientific computations. This programming language includes an environment for data visualization, computations, and logic codings. It also includes inbuilt error debugging and editing tools, and it can support the idea of object-oriented programming (OOP). These parameters make Matlab great use and use these tools for academic research and teaching. • You might be familiar with the concept of Matlab, and its basic operation, data types, and loops. This post will provide you the details of the switch case in Matlab. The switch case statement is used to control the flow logic that can be used extensively. You can use several alternatives using the If, If-else statements. This statement can make the code lengthy and debug and not easy to read. Switch case statement helps the programmers to select the number of options that makes the code easy to read, less time-consuming, but the way of implementation can be varied. • What is the switch case in Matlab?

  3. Syntax of a switch case in Matlab • switch • switch_condition • case case_condition • statements_if_true • case case_condition • statements_if_true • …Otherwise_condition • Statements_if_true • End

  4. Explanation • The initial two lines, which are “switch_condition,case_condition, end” are used to execute the expression and make the options to implement one of the various statements. Each option is called a case.the each block of switch case in Matlab use to perform the check each case statement until any of the expressions is not found as true. Here the true statement means the condition must satisfy the statement: • For the numeric values, the case_statement and switch_statement are equal to each other. • For the character data types, the strcmp(case_statement, switch_statement) will be equal to 1. • The object can be supported by the equations, such as case_statement, the function must be equal to the switch_statement. • To represent the cell array data type structure, the elements of the cell array must be matched with the switch_statement. • If any of the case statements is true, then the next statement would be executed by Matlab, and then it will come out to the switch statement.  • To work properly, the implemented switch_statement must be a character vector data type or scalar data type. • When no case block is found to be true, the blocks will be considered as optional and execute accordingly.

  5. Flow diagram of Switch case in Matlab • As you have the idea about the syntax of the switch case in Matlab, now the flow diagram will make it easier to understand. If you go through with each statement block, then you can understand the working processor of the switch case statement. It is the power of the flow diagram and why it is necessary to teach the complications of the programming. Using the flow diagram, you can frame your design and logics of error-free coding before actually writing it on the system.

  6. The working processor of switch case in Matlab • The switch case statement is used to execute the set of the statement(s) that are based on the criteria of the various choices. Each of the options is covered with the case statement. There are several things that can be noticeable that the switch case in Matlab has a little bit different than the other coding languages. Let’s take an example of it, Matlab switch case statement can construct some of the body conditions in a different way as that of the switch condition of the C coding language. • The switch case statement of the C programming is used to construct the execution that allows you to go through several case before the execution of the actual programming. The switch case of the C program can control its execution using the break statement. But in the case of Matlab, the switch case is used to execute a case group, and it does not need the break statements. • 15

  7. Example 1: • This example can do a simple job as it uses to pass the switch case statement and then print the messages which are based on the particular condition. You can generate a logic of match numbers and offer the result on the basis of that number. • Let’s take some of the examples of switch case in Matlab so that you can understand the working of the switch case statements and execute the program accordingly. • Examples • 14

  8. N = input(‘Write a number of your choice: ‘); • switch N • case 4 • disp(‘positive  one selected’) • case 0 • disp(‘zero selected’)case -4 • disp(‘negative one selected’) • otherwise • disp(‘Some other value’) • end • Output: –As the command executes, enter the number 4. • Positive four • Write the code and enter the number 7. • Some other value • Example 1 • 16

  9. Enter_grade = ‘A’; • switch(enter_grade) • case ‘A+' • fprintf(‘Excellent!\n’ ); • case ‘B+’ • fprintf(‘Well done\n’ ); • case ‘C+’ • fprintf(‘Very Good\n’ ); • case ‘D+ • ’fprintf(‘You passed..\n’ ); • case ‘F’fprintf(‘Better luck next time\n’ ); • otherwise • fprintf(‘Invalid grade number. Please enter correct grade value\n’ ); • End • Example 2 • 16

  10. Output • When this program is executed, it will display the value as ‘excellent’ as an initial line of code is written as ‘A+’. By replacing the value of ‘A+’ with ‘B+’ and execute, it will show the result as ‘well-done’. Therefore, we can say that as the input grade value, the result will be shown, and all these programs can be executed up to 10 lines of the code. If the programmer runs the above-mentioned program using ‘if-else’ logics, then the code would be much longer and has the additional conditional statements too. • 17

  11. This article has provided information on the switch case in Matlab that covers the meaning of a switch case, syntax, flow diagram, working processor, and some of the examples of the switch case statements. Students need to learn these things and execute the code to practice the programming. This will help you to make that you have understood the concepts of this programming that will develop the confidence to write error-free programs. • Conclusion

  12. Facebook • Twitter • Pinterest • @matlabassignmenthelp1 • @matlabassignme5 • @matlabassignmenthelp3

  13. Website • Get in Touch With Us www.matlabassignmenthelp.com Email info@matlabassignmenthelp.com

  14. MATLABASSIGNMENTHELP • Thanks For Watching • Like and Share

More Related