当前位置:网站首页>matlab as(assert dominance)
matlab as(assert dominance)
2022-07-31 12:50:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
目录
assert是条件为 false 时引发错误。
语法
assert(cond)
assert(cond,msg)
assert(cond,msg,A1,...,An)
assert(cond,errID,msg)
assert(cond,errID,msg,A1,...,An) 断言条件,指定为有效的 MATLAB 表达式。如果 cond 为 false,assert 函数会引发错误。cond 可以包含关系运算符(例如 < 或 ==)和逻辑运算符(例如 &&、|| 或 ~)。使用逻辑运算符 and 和 or 创建复合表达式。MATLAB 按照运算符优先级规则从左至右计算复合表达式。
注意
如果希望 MATLAB 转换错误消息中的特殊字符(例如 \t、\n、%s 和 %d),必须为 assert 指定多个输入参数。数值、字符或字符串数组。此输入参数提供与 msg 中的转换设定符相对应的值,用来替换这些转换设定符。
提示
- 引发错误时,MATLAB 会捕获该错误的相关信息并将其存储在用作
MException类的对象的数据结构体中。可以使用try/catch来访问异常对象中的信息。或者,如果程序由于异常而终止,并将控制权返回至命令提示符,则可以使用MException.last。 - 如果断言失败发生在try块内,则 MATLAB 不会停止执行该程序。在本例中,MATLAB 将控件传递到catch块中。
说明
如果 cond 为 false,assert(cond) 会引发错误。
如果 cond 为 false,assert(cond,msg) 会引发错误并显示错误消息 msg。
如果 cond 为 false,assert(cond,msg,A1,…,An) 会显示一条错误消息,其中包含格式设置转换字符,例如与 MATLAB sprintf 函数一起使用的字符。msg 中的每个转换字符都会转换为 A1,…,An 中的一个值。
如果cond 为 false,assert(cond,errID,msg) 会抛出错误并显示错误消息 msg,还会包含一个标示此异常的错误标识符。此标识符可用于区分错误,它还允许您控制在 MATLAB 遇到错误时系统做何反应。
assert(cond,errID,msg,A1,…,An) 会显示格式化的错误消息,还会包含标示异常的错误标识符。
示例
值在预期范围之内
断言值 x 大于指定的最小值。
minVal = 7;
x = 26;
assert(minVal < x) 表达式的计算结果为 true,断言通过。断言 x 的值介于指定的最小值和最大值之间。
maxVal = 13;
assert((minVal < x) && (x < maxVal))
Assertion failed.表达式的计算结果为 false。断言失败,MATLAB 引发错误。
期望的数据类型
断言两个数字的乘积为双精度值。
a = 13;
b = single(42);
c = a*b;
assert(isa(c,'double'),'Product is not type double.')
Product is not type double.增强错误消息以便显示c的数据类型。
assert(isa(c,'double'),'Product is type %s, not double.',class(c))
Product is type single, not double.预期的代码执行情况
使用assert函数测试在正常的代码执行情况下不应出现的情况。如果系数为数值,计算的根也应为数值。使用指定系数和计算根的二次方程计算结果应为零。
function x = quadraticSolver(C)
validateattributes(C,{'numeric'},{'size',[1 3]})
a = C(1);
b = C(2);
c = C(3);
x(1) = (-b+sqrt(b^2-4*a*c))/(2*a);
x(2) = (-b-sqrt(b^2-4*a*c))/(2*a);
assert(isnumeric(x),'quadraticSolver:nonnumericRoots',...
'Computed roots are not numeric')
y1 = a*x(1)^2+b*x(1)+c;
y2 = a*x(2)^2+b*x(2)+c;
assert(y1 == 0,'quadraticSolver:root1Error','Error in first root')
assert(isequal(y2,0),'quadraticSolver:root2Error','Error in second root')
end发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/128640.html原文链接:https://javaforall.cn
边栏推荐
- PAT exam summary (exam experience)
- Banyan Tree Loan GPU Hardware Architecture
- sqlalchemy determines whether a field of type array has at least one consistent data with an array
- chroot命令
- SAP 电商云 Spartacus UI 和 Accelerator UI 里的 ASM 模块
- Use IN List Population in Your JDBC Application to Avoid Cursor Cache Contention Issues
- 0x80070570文件或目录损坏且无法删除(0x80070091怎么删除)
- 双非一本进字节了!!纯干货分享
- 基本语法(二)
- FastAPI 封装一个通用的response
猜你喜欢

荣耀手机参数写错,客服认为没错

跨境电商小知识之跨境电商物流定义以及方式讲解

How does the SAP ABAP OData service support the $filter (filter) operation trial version

AMBA APB学习记录(AMBA 2.0)

这款悄然崛起的国产API接口管理工具,你一定要晓得

攻防演练丨赛宁红方管控平台走进广东三地 助力数字政府网络安全建设

PyQt5 rapid development and actual combat 9.7 Automated testing of UI layer

消息队列面试题(2022最新整理)

anaconda虚拟环境安装pytorch gpu版本

机器学习基本概念
随机推荐
MySQL面试八股文(2022最新整理)
Centos7 install mysql5.7 steps (graphical version)
ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法
jmeter性能测试步骤入门(性能测试工具jmeter)
FastAPI 封装一个通用的response
SAP 电商云 Spartacus UI 和 Accelerator UI 里的 ASM 模块
建情人节表白网站(超详细过程,包教包会)
Structural controllability of switched linear systems with symmetry constraints
关于我放弃考研这件事儿
Fully Dynamically Constrained Robot Efficient Time-Optimal Trajectory Planning
Hard disk partition, expand disk C, no reshipment system, not heavy D dish of software full tutorial.
带有对称约束切换线性系统的结构可控性
荣耀手机参数写错,客服认为没错
快速学完数据库管理
Indoor real-time laser SLAM control method based on biological excitation neural network
CentOS7 - yum install mysql
busybox之reboot命令流程分析
SAP ABAP OData 服务如何支持 $filter (过滤)操作试读版
WPF中报错:“未将对象引用设置到对象的实例。”
消息队列面试题(2022最新整理)