- You do not call the function you edit. E.g. there could be another function with the same name in another folder of Matlab's path.
- It is the right function, but you did not save it after some changes. Then you do not run the version you see in the editor.
- In opposite to your expectations, the condition is not TRUE.
- You have redefined the function "error", such that the original function is shadowed. Then error() is called, but Matlab performs something else.
Making a function produce a specific error?
1 次查看(过去 30 天)
显示 更早的评论
I have a certain function that I created and I need it to produce a certain error when a certain condition is satisfied. For example:
if x > a(1)
error('Error using function y')
end
However, for some reason, when I run the function, the condition is not checked and instead the function computes an output though the condition for the if statement should have been satisfied.
0 个评论
采纳的回答
Jan
2013-4-6
Matlab is a deterministic language. When this line of code is reached, the condition is checked reliably. And if the condition is true, you can be sure, that the error appears. So there are several possible causes for your observations:
You can check all these ideas easily: Simply set a breakpoint in this line, start Matlab again, and when the line is reached, check the values of x and a(1). Then step forward one line and step into the error function on demand. If the breakpoint is not reached, you call another function.
But in any way you can be sure, that Matlab does exactly what it is instructed to do. There is no magic avoidance of commands or lazy decisions not to step into the if-clause.
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Debugging and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!