switch

Just like an if-else statement but can only compare one variable. Each possibility is represented with a 'case' with the default case being 'otherwise'.

Syntax

switch (X)
  case a
    statement
  case b
    otherStatement;
  otherwise
    otherOtherStatement;
 end

Inputs

case
Logical case for each possible switch statement.
Type: double | char | logical | struct | cell | integer
Dimension: scalar | vector | matrix
otherwise
Default case if none of the other case conditions are met.
Type: double | char | logical | struct | cell | integer
Dimension: scalar | vector | matrix

Outputs

statement
Executed if case condition is met.

Example

Simple switch statement:

switch (X)
  case 1
    print ('X = 1');
  case 2
    print ('X = 2');
  otherwise
    print ('X does not equal 1 or 2');
end