Making a function produce a specific error?

조회 수: 1 (최근 30일)
Joe
Joe 2013년 4월 6일
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.

채택된 답변

Jan
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:
  1. 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.
  2. 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.
  3. In opposite to your expectations, the condition is not TRUE.
  4. You have redefined the function "error", such that the original function is shadowed. Then error() is called, but Matlab performs something else.
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개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by