前文中,我们说了Matlab使用for编写循环语句,使用if编写判断语句,使用while编写循环执行语句。今天,我们再带来一篇使用switch来判断多条语句中判断需要执行的语句。你可以理解为给定一个值,判断该值的大小,根据不同的值,输出不同的语句。当然,跟通俗的理解为:多个值的判断比较。
本文,我们主要讲解Matlab中switch函数的常见用法、语法说明、比较单个值、与多个值进行比较。当然,本文对Matlab编程与数据处理方面,都有所帮助。
下面,我们首先给出Matlab中关于switch函数的帮助文档如下:
>> help switch switch Switch among several cases based on expression. The general form of the switch statement is: switch switch_expr CASE case_expr, statement, ..., statement CASE {case_expr1, case_expr2, case_expr3,...} statement, ..., statement ... OTHERWISE, statement, ..., statement END The statements following the first CASE where the switch_expr matches the case_expr are executed. When the case expression is a cell array (as in the second case above), the case_expr matches if any of the elements of the cell array match the switch expression. If none of the case expressions match the switch expression then the OTHERWISE case is executed (if it exists). Only one CASE is executed and execution resumes with the statement after the END. The switch_expr can be a scalar or a string. A scalar switch_expr matches a case_expr if switch_expr==case_expr. A string switch_expr matches a case_expr if strcmp(switch_expr,case_expr) returns 1 (true). Only the statements between the matching CASE and the next CASE, OTHERWISE, or END are executed. Unlike C, the switch statement does not fall through (so BREAKs are unnecessary). Example: To execute a certain block of code based on what the string, METHOD, is set to, method = 'Bilinear'; switch lower(method) case {'linear','bilinear'} disp('Method is linear') case 'cubic' disp('Method is cubic') case 'nearest' disp('Method is nearest') otherwise disp('Unknown method.') end Method is linear
常见用法
switch switch_expression case case_expression statements case case_expression statements ... otherwise statements end
语法说明
switch switch_expression, case case_expression, end 计算表达式并选择执行多组语句中的一组。每个选项为一个 case。
switch 块会测试每个 case,直至一个 case 表达式为 true。case 在以下情况下为 true:
- 对于数字,case_expression == switch_expression。
- 对于字符向量,strcmp(case_expression,switch_expression) == 1。
- 对于支持 eq 函数的对象,case_expression == switch_expression。重载的 eq 函数的输出必须为逻辑值或可转换为逻辑值。
- 对于元胞数组 case_expression,元胞数组的至少一个元素与 switch_expression 匹配,如上述对数字、字符向量和对象的定义。
当 case 表达式为 true 时,MATLAB® 执行对应的语句,然后退出 switch 块。
计算的 switch_expression 必须为标量或字符向量。计算的 case_expression 必须为标量、字符向量或者标量或字符向量的元胞数组。
otherwise 块是可选的。仅当没有 case 为 true 时,MATLAB 才会执行这些语句。
比较单个值
根据在命令提示符下输入的值有条件地显示不同的文本。
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
在命令提示符下,输入数字 1。输出结果如下:
positive one
重复执行该代码并输入数字 3。输出结果如下:
other value
与多个值进行比较
基于 plottype 的值确定要创建哪种类型的绘图。如果 plottype 为 ‘pie’ 或 ‘pie3’,则创建一个三维饼图。使用元胞数组包含两个值。
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
友情提示
case_expression 不能包含关系运算符(例如 < 或 >)来与 switch_expression 进行比较。要测试不相等性,请使用 if, elseif, else 语句。
MATLAB switch 语句不会像 C 语言的 switch 语句一样失效。如果第一个 case 语句为 true,则 MATLAB 不会执行其他 case 语句。例如:
result = 52; switch(result) case 52 disp('result is 52') case {52, 78} disp('result is 52 or 78') end result is 52
在该 case 内定义特定 case 中的代码所需要的变量。由于 MATLAB 仅执行任何 switch 语句的一个 case,因此一个 case 内定义的变量不适用于其他 case。例如,如果当前工作区不包含变量 x,则仅定义 x 的情况可以使用它:
switch choice case 1 x = -pi:0.01:pi; case 2 % does not know anything about x end
MATLAB break 语句会结束 for 或 while 循环的执行,但不结束 switch 语句的执行。此行为不同于 C 语言中 break 和 switch 的行为。
暂无评论内容