matlab中switch嵌套使用用法,求matlab中的switch模块的用法?

满意答案

dcebd7a0de6265b6ccae5ead692f1eab.png

hjkhkj0

2013.06.04

dcebd7a0de6265b6ccae5ead692f1eab.png

采纳率:42%    等级:13

已帮助:10338人

switch

Switch among several cases, based on expression

Syntax

switch switch_expr

case case_expr

statement, ..., statement

case {case_expr1, case_expr2, case_expr3, ...}

statement, ..., statement

otherwise

statement, ..., statement

end

Discussion

The switch statement syntax is a means of conditionally executing code. In particular, switch executes one set of statements selected from an arbitrary number of alternatives. Each alternative is called a case, and consists of

*

The case statement

*

One or more case expressions

*

One or more statements

In its basic syntax, switch executes the statements associated with the first case where switch_expr == case_expr. When the case expression is a cell array (as in the second case above), switch executes the case where any of the elements of the cell array matches the switch expression. If no case expression matches the switch expression, then control passes to the otherwise case (if it exists). After the case is executed, program 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 logical 1 (true).

A case_expr can include arithmetic or logical operators, but not relational operators such as < or >. To test for inequality, use if-elseif statements.

Note for C Programmers Unlike the C language switch construct, the MATLAB switch does not "fall through." That is, switch executes only the first matching case; subsequent matching cases do not execute. Therefore, break statements are not used.

Examples

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

See Also

case, otherwise, end, if, else, elseif, while

09分享举报

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值