A switch block conditionally executes one set of statements from several choices. Each choice is covered by a case statement.
An evaluated switchexpression is a scalar or string.
An evaluated caseexpression is a scalar, a string or a cell array of scalars or strings.
The switch block tests each case until one of the cases is true. A case is true when:
• For numbers, eq(caseexpression,switchexpression).
• For strings, strcmp(caseexpression,switchexpression).
• For objects that support the eq function, eq(caseexpression,switchexpression).
• For a cell array caseexpression, at least one of the elements of the cell array matches switchexpression, as defined above for numbers, strings and objects.
When a case is true, MATLAB executes the corresponding statements and then exits the switch block.
The otherwise block is optional and executes only when no case is true.