The switch statement in MATLAB offers an organized approach to execute various code blocks depending on the result of an expression. This article aims to provide a comprehensive understanding of the switch statement in MATLAB, including its syntax and various examples to demonstrate its usage and versatility.
switch in MATLAB
In MATLAB, the switch statement is a control flow component that enables you to run various code blocks based on the result of an expression. The switch statement evaluates the expression and compares it against different cases, executing the code block that corresponds to a matching case, syntax for the switch statement in MATLAB is as follows:
case value1
% code block to execute for value1
case value2
% code block to execute for value2
case value3
% code block to execute for value3
...
otherwise
% code block to execute if none of the cases match
end
The matching code block is performed once the expression has been evaluated and depending on its result. Each case contains a unique value to compare to the expression, and if a match is made, the corresponding code block is run. If none of the case values fit the expression, the optional else (otherwise) block is executed. Here are several examples to understand the usage and flexibility of the switch statement in MATLAB:
Example 1: Numeric Cases
switch value
case 1
disp('Value is 1');
case 2
disp('Value is 2');
case 3
disp('Value is 3');
otherwise
disp('Value is not 1, 2, or 3');
end
In this example, the switch statement evaluates the value of the variable value. Since the value is 3, the corresponding case Value is 3 is executed, and the output is displayed:
Example 2: Character Cases
switch color
case 'r'
disp('Color is red');
case 'g'
disp('Color is green');
case 'b'
disp('Color is blue');
otherwise
disp('Unknown color');
end
In this example, the switch statement evaluates the character variable color. As the color is r, the case Color is red is executed, and the corresponding output is displayed:
Example 3: String Cases
switch fruit
case 'apple'
disp('It is an apple');
case 'banana'
disp('It is a banana');
case 'orange'
disp('It is an orange');
otherwise
disp('Unknown fruit');
end
In this example, the switch statement evaluates the string variable fruit. As fruit is an apple, the case It is an apple is executed, and the corresponding output is displayed:
Example 4: Multiple Cases
switch value
case 1
case 2
disp('Value is either 1 or 2');
case {3, 4, 5}
disp('Value is either 3, 4, or 5');
otherwise
disp('Value is not 1, 2, 3, 4, or 5');
end
In this example, the switch statement checks for multiple cases using the curly braces {}. As the value is 5, which matches the case {3, 4, 5}, the corresponding code block Value is either 3, 4, or 5 is executed, and the output is displayed:
Conclusion
The switch statement in MATLAB provides a structured way to handle different cases based on the value of an expression. By evaluating the expression and comparing it to specific cases, you can execute the corresponding code block that matches the value. The switch statement offers flexibility in handling numeric values, characters, and strings, allowing you to build conditional logic based on various scenarios.