switch - Execute one of several groups of statements - MATLAB (original) (raw)
switch, case, otherwise
Execute one of several groups of statements
Syntax
switch switchexpression
case caseexpression
statements
case caseexpression
statements
...
otherwise
statements
end
Description
switch _`switchexpression`_, case _`caseexpression`_, end
evaluates an expression and chooses to execute one of several groups of statements. Each choice is a case.
The switch
block tests each case until one of the case expressions is true. A case is true when:
- For numbers,
_`caseexpression`_ == _`switchexpression`_
. - For character vectors and strings,
strcmp(_`caseexpression`_,_`switchexpression`_) == 1
. Cell arrays of character vectors are a special case wherecase
will match if at least one element of the cell array matches. - For objects that support the
eq
function,_`caseexpression`_ ==_`switchexpression`_
. The output of the overloadedeq
function must be either a logical value or convertible to a logical value. - For a cell array
caseexpression
, at least one of the elements of the cell array matchesswitchexpression
, as defined above for numbers, character vectors, and objects.
When a case expression is true, MATLABĀ® executes the corresponding statements and exits the switch
block.
An evaluated switchexpression
must be a scalar or character vector. An evaluated caseexpression
must be a scalar, a character vector, or a cell array of scalars or character vectors.
The otherwise
block is optional. MATLAB executes the statements only when no case is true.
Examples
Display different text conditionally, depending on a value entered at the command prompt.
n = input('Enter a number: ');
switch n case -1 disp('negative one') case 0 disp('zero') case 1 disp('positive one') otherwise disp('other value') end
At the command prompt, enter the number 1.
Repeat the code and enter the number 3.
Determine which type of plot to create based on the value of plottype
. If plottype
is either 'pie'
or 'pie3'
, create a 3-D pie chart. Use a cell array to contain both values.
x = [12 64 24]; plottype = 'pie3';
switch plottype case 'bar' bar(x) title('Bar Graph') case {'pie','pie3'} pie3(x) title('Pie Chart') otherwise warning('Unexpected plot type. No plot created.') end
Tips
- A
caseexpression
cannot include relational operators such as<
or>
for comparison against theswitchexpression
. To test for inequality, use if, elseif, else statements. - The MATLAB
switch
statement does not fall through like a C languageswitch
statement. If the firstcase
statement istrue
, MATLAB does not execute the othercase
statements. For example:
result = 52;
switch(result)
case 52
disp('result is 52')
case {52, 78}
disp('result is 52 or 78')
end
- Define all variables necessary for code in a particular case within that case. Since MATLAB executes only one case of any
switch
statement, variables defined within one case are not available for other cases. For example, if your current workspace does not contain a variablex
, only cases that definex
can use it:
switch choice
case 1
x = -pi:0.01:pi;
case 2
% does not know anything about x
end
- The MATLAB
break
statement ends execution of afor
orwhile
loop, but does not end execution of aswitch
statement. This behavior is different than the behavior ofbreak
andswitch
in C.
Extended Capabilities
Usage notes and limitations:
If all case expressions are scalar integer values, then the code generator produces a C
switch
statement. At run time, if the switch value is not an integer, then the code generator produces an error.When the case expressions contain noninteger or nonscalar values, the code generator produces C
if
statements in place of a Cswitch
statement.The conditional expression in a
switch
orcase
statement must use only:uint8
,uint16
,uint32
,int8
,int16
, orint32
data types- Scalar data
If multiple
case
statements make assignments to the same variable, the numeric type andfimath
specification for that variable must be the same in everycase
statement.
Version History
Introduced before R2006a