FUNCTION REFERENCE

switch

Choose a block of statements by value.

Choose a block of statements by value.

Syntax

switch expr; case value; statements; otherwise; statements; end

Compares expr with each case value in turn and runs the first block that matches; at most one block runs. Numbers match by value, character rows match exactly, and a cell array of values matches when any element does. A single character compared with a number compares by character code. Use otherwise for the block that runs when nothing matches.

Example

x = 2; switch x; case 1; disp('one'); case {2, 3}; disp('two or three'); otherwise; disp('other'); end   % two or three

Differences from MATLAB: Multi-character text never matches a number.

These are the engine’s help pages. Each page keeps its compatibility notes. File operations are unavailable in the browser.