MATLAB编程中的错误处理与函数编写技巧
1. 错误类型与陷阱
1.1 语法错误
语法错误是MATLAB语句中的输入错误,是最常见的错误类型,会导致MATLAB停止执行并显示错误信息。以下是一些常见的语法错误示例:
| 错误信息 | 示例代码 | 错误原因 |
| — | — | — |
| “)” expected, “end of line” found. | 2*(1+3 | 缺少右括号 |
| “end” expected, “End of Input” found. | x = input( 'x: ' ); if x < 0 disp( 'negative' ) else if x == 0 disp( 'zero' ) else disp( 'positive' ) end | else if 应写成 elseif |
| Error using ==> * Inner matrix dimensions must agree. | [1 2 3] * [4 5 6] | 矩阵乘法要求内维数一致 |
| Missing operator, comma, or semicolon. | if x < 0 disp('negative') end | disp 语句后缺少逗号 |
| Expected a variable, function, or constant, found … |
超级会员免费看
订阅专栏 解锁全文
6873

被折叠的 条评论
为什么被折叠?



