EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial Switch Statement in MATLAB
 

Switch Statement in MATLAB

Priya Pedamkar
Article byPriya Pedamkar

Updated March 20, 2023

Switch Statement in MATLAB

 

 

What is MATLAB Switch Statement?

Switch statements in MATLAB are a valuable feature that programmers use to execute different operations based on the value of a variable. The switch statement begins with an expression and compares it to a list of cases. Each case is a possible value for the expression, and when the switch statement identifies a case that matches the expression, it runs a block of code associated with that case.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

In comparison to a series of if-else statements, switch statements are more efficient and simpler to write. By using switch statements, MATLAB programmers can write more concise and readable code that performs various operations based on the value of a variable.

Syntax of Switch Statement in MATLAB

In this section, we have provided the standard syntax of the switch statements and a step-by-step explanation.

switch switch_condition
case case_condition
statements_if_true
case case_condition
statements_if_true
...
Otherwise_condition
Statements_if_true
end

Explanation:

The first two lines “switch_ condition, case_ condition” performs an evaluation of an expression and then make a choice for executing one of several statements. Each of such choices is called a case.

The switch block performs the test on each case until one of the case expressions is found to be true. A case is termed true when it satisfies:

  • In the case of numbers, its case_ condition is equal to the switch_ condition.
  • In the case of character data types, its strcmp(case_ condition, switch_ condition) is equal to 1.
  • In the case of objects which support the equation like function, case_ condition should be equal to switch_ condition.
  • In the case of cell array type data structure, a minimum of one of the given elements of the cell array will match with the given switch_ condition.
  • When a case condition is found to be true then MATLAB executes that statement and after execution comes out of the switch
  • For it to work properly, the evaluated switch_ condition should be scalar data type or character vector data type.
  • The otherwise block is optional and is executed when no case is found to be true.

Flow Diagram of Switch Statement in MATLAB

This section provides the flow diagram for the above syntax to understand it easily. If you read each block of statements, you can understand the working of a switch. This flow diagram will help to frame your logic and design error-free code before writing it.

matlab Flowchart

 

How does Switch Statement work in MATLAB?

So far we have understood the technical details of the switch statement. Let’s focus more on its working.

A switch block, as mentioned previously, conditionally executes one set of statements based on criteria from several choices. Each of these choices is covered by a case statement.

When we look at the below examples, the construct and function of the switch statement will be cleared. The switch statement in MATLAB may differ slightly from that of other programming languages. For example, the MATLAB switch construct is different in some respects from the C programming language construct of the switch. The C switch construct allows execution to go through many case groups before its execution halts. We can use break statements in C to control the execution. The concept is different in MATLAB, where the switch construct only executes one case group and thus does not require break statements.

Examples of Switch Statements in MATLAB

Let us look at some of the examples of the switch statements in MATLAB.

Example #1

This example performs a simple task. The basic idea is to run a switch statement and print a message based on a condition. We create a logic for matching the number and providing an output based on the number.

Code:

N = input('Enter a number of your choice: ');
switch N
case -2
disp('negative one selected')
case 0
disp('zero selected')
case 2
disp('positive one selected')
otherwise
disp('Some other value')
end

Output:

Enter the number -2, the output will be as shown below,

Switch Statement in Matlab Example 1-1

Repeat the code and enter the number 5, the output will be as shown below,

Switch Statement in Matlab Example 1-2

Repeat the code and enter the number 2, the output will be as shown below,

Enter 2 Example 1-3

Example #2

In this example of the switch statement in MATLAB, we have classified the distinction based on the grade obtained.

Code:

Enter_grade = 'A';
switch(Enter_grade)
case 'A'
fprintf('Excellent performance!\n' );
case 'B'
fprintf('Well done performance\n' );
case 'C'
fprintf('Very Good performance\n' );
case 'D'
fprintf('You passed.. Congratulations\n' );
case 'F'
fprintf('Better luck next time\n' );
otherwise
fprintf('Invalid grade. Please enter correct value\n' );
end

Output:

Switch Statement in Matlab Example 2

After running, it will display Excellent Performance! because the grade selected in the first line of code is A. Replace A with B and run again, the output will be Well-done Performance. Based on the grade selected, the distinction comes, and all of this is possible in just 10 lines of code. Consider using the same if-else logic, the code will be much longer and will contain many additional conditional statements.

Conclusion

In this article, we have covered the switch statements in MATLAB and provided examples with codes to demonstrate how they work. Students should learn and practice coding as much as possible. It will ensure that concepts are well understood and they are confident in writing error-free code. If there are any questions or if they encounter errors while executing code, they can comment or contact us for more information. For more examples and case studies using switch statements, students can use google search or refer to the official MATLAB documentation. It is always best to refer to the official documentation as it provides a comprehensive discussion and numerous edge cases for relevant scenarios.

Recommended Articles

This was a guide on the Switch Statement in MATLAB. We discussed the fundamental concepts, how it works with examples, and flow diagrams of the Switch Statement in Matlab. You can also learn more by reading our other other articles. –

  1. Python vs Matlab
  2. Matlab Operators
  3. Top 6 AWS Alternatives
  4. What is Case Statement in Java?
  5. Matlab Compiler | Applications of Matlab Compiler

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW