matlab中的case语句,MATLAB switch语句
本文概述
开关是另一种条件语句, 它执行多个语句组中的一个。
如果我们要根据一组预定义的规则测试相等性, 那么switch语句可以替代if语句。
句法
switch switch_expression
case case_expression1
Statements
case case_expression2
Statements
case case_expressionN
Statements
otherwise
Statements
End
Switch语句流程图
以下是在MATLAB中使用switch的要点:
类似于if块, switch块会测试每种情况, 直到case_expression之一为true为止。评估为:
case&switch必须等于数字ascase_expression == switch_expression。
对于字符向量, strcmp函数返回的结果必须等于1, 因为-strcmp(case_expression, switch_expression)== 1。
对于对象, case_expression == switch_expression。
对于单元阵列, case_expression中的单元阵列的至少一个元素必须与switch_expression匹配。
switch语句不测试不等式, 因此case_expression不能包含诸如之类的关系运算符以与switch_expression进行比较。
例1
% program to check whether the entered number is a weekday or not
a = input('enter a number : ')
switch a
case 1
disp('Monday')
case 2
disp('Tuesday')
case 3
disp('Wednesday')
case 4
disp('Thursday')
case 5
disp('Friday')
case 6
disp('Saturday')
case 7
disp('Sunday')
otherwise
disp('not a weekday')
end
输出
enter a number: 4
Thursday
例2
% Program to find the weekday of a given date
% take date input from keyboard in the specified format
d = input('enter a date in the format- dd-mmm-yyyy:', "s")
% weekday function takes input argument
% and returns the day number & day name of the particular date.
[dayNumber, dayName] = weekday(d, 'long', "local");
% use switch and case to display the output as per the entered input
switch dayNumber
case 2
x = sprintf('Start of the week-%s-:%d', dayName, dayNumber);
disp(x)
case 3
x = sprintf('%s:%d', dayName, dayNumber);
disp(x)
case 4
x = sprintf('%s:%d', dayName, dayNumber);
disp(x)
case 5
x = sprintf('%s:%d', dayName, dayNumber);
disp(x)
case 6
x = sprintf('Last working day-%s-of the week:%d', dayName, dayNumber);
disp(x)
otherwise
disp('weekend')
end
matlab中的case语句,MATLAB switch语句相关推荐
- matlab if语句多个执行举例,初学Matlab,有两个语句,if语句和switch语句,有两个例子哪位大神能帮我讲讲...
问题描述: 初学Matlab,有两个语句,if语句和switch语句,有两个例子哪位大神能帮我讲讲 if logical_expression statements elseif logical_ex ...
- java中if结构用图表示_Java语法基础之选择结构的if语句、switch语句详解
[前言] 流程控制语句: 在一个程序执行的过程中,各条语句的执行顺序对程序的结果是有直接影响的.也就是说程序的流程对运行结果有直接的影响.所以,我们必须清楚每条语句的执行流程.而且,很多时候我们要通过 ...
- Python中如何优雅地使用switch语句
文章目录 Python中如何优雅地使用switch语句 案例一(简单情况) 案例二(带条件判断) 版权声明:本文为博主原创文章,转载请注明原文出处! 写作时间:2019-03-07 13:49:45 ...
- matlab中集合的表示,Matlab常用命令集合
1.常用命令-->管理命令和函数 addpath添加目录到MATLAB搜索路径doc在Web浏览器上现实HTML文档help显示Matlab命令和M文件的在线帮助helpwin helpdesk ...
- if break语句_8、嵌套if语句、switch语句
1.嵌套if语句 嵌套if语句是指在 if...else分支中还存在if...else 语句. 不大难,题目后期当练习再补. 2.switch语句 示例3.20: 看起来不难,先判断是否是2种情况,不 ...
- java对比swith和if的不同,浅谈选择结构if语句和switch语句的区别
1.选择结构if语句格式及其使用 A:if语句的格式: if(比较表达式1) { 语句体1; }else if(比较表达式2) { 语句体2; }else if(比较表达式3) { 语句体3; } . ...
- if 语句与switch语句
目录 一.if语句 1.if语句-单分支结构 2.if-else语句-双分支结构 3.if-else if-else语句-多分支结构 4.if语句的嵌套 二.switch语句 三.if语句与switc ...
- if语句和switch语句
文章目录 前言 一.if语句 二.switch语句 总结 前言 如果条件满足才能做某件事情,如果条件不满足就做另外一件事情,或者什么也不做.分支语句又被称为"判断语句",正因为有了 ...
- c语言入门----详解分支语句(switch语句)
目录标题 前言 一.为什么会有switch语句 二.switch语句的基本形式 三.switch语句中的break 1.为什么会有break语句 2.break语句的使用 四.switch语句中的de ...
- c语言switch中用ifelse,初学者求问:用if单分支选择语句替换switch语句?if.else呢...
菜鸟求问:用if单分支选择语句替换switch语句?if...else呢? 主要是关于default情况的处理,代码如下: /* Fig. 4.7: fig04_07.c Counting lette ...
最新文章
- 聊天机器人革命即将到来 开发者入局需谨慎
- 一张图搞定Android学习路线
- 传输层 :TCP/UDP 协议(用于封装接口)
- ubuntu如何进入超级权限模式,退出超级权限模式,sudo su——root权限
- 【Docker】Docker 启动prometheus报错 parsing YAML file /etc/prometheus/prometheus.yml: yaml: unmarshal
- App后台开发运维和架构实践学习总结(4)——APP的注册和登录功能设计
- android 获取GPS定位,
- HTTP 请求头(首部)一览表
- pytorch中模型结构图的可视化
- AssetBundle资源冗余检测
- windows - cmd常用操作命令 (dos窗口)
- 第二章:上下文无关文法
- Pytorch中报错RuntimeError: The size of tensor a (60) must match the size of tensor b (56)
- 微型打印机方案(包含原理图、PCB和BOM表)
- 笔记本独显无输出_双显卡笔记本独显消失怎么回事|笔记本双显卡独显没了怎么解决|笔记本双显卡切换独显方法...
- 惜我者,我惜之; 冷我者,我弃之
- fzoj Problem 2190 非提的救赎 【单调栈】
- web和http协议-详解
- 如何根据图片换算出中轴大小?
- ISP—去马赛克和颜色空间变换对图像噪声的影响