Effacer les filtres
Effacer les filtres

Making a function produce a specific error?

1 vue (au cours des 30 derniers jours)
Joe
Joe le 6 Avr 2013
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.

Réponse acceptée

Jan
Jan le 6 Avr 2013
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.

Plus de réponses (0)

Catégories

En savoir plus sur Debugging and Analysis dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by