11.6) switch...case...otherwise Statement
- The switch...case...otherwise construct allows a statement from a set of statements to be executed depending upon the value of a switch expression. The syntax is as follows:
- The switch...case...otherwise construct is similar to the if...elseif...end construct, but it can be more readable, especially if the statements contain if...end statements
- In some circumstances it may be more flexible, if there is more than one value for each choice.
[matlab]
switch switch_statement
case case_expression1
expression1
case case_expression2
expression2
otherwise
expression3
end
[/matlab]
[matlab]
>> % Use switch... rather than if... statements
>> plot_type=2
>> data=1:100;
>> data(2,:)=rand(1,100);
>> switch plot_type
>> case 1
>> scatter(data(1,:),data(2,:))
>> case 2
>> line(data(1,:),data(2,:))
>> otherwise
>> display('plot type unknown')
>> end
[/matlab]