Assertions in matlab

3 visualizaciones (últimos 30 días)
Mohan
Mohan el 6 de Dic. de 2011
Hello,
I am very new to matlab, I started using since a month. I want to know how assertions are handled in matlab and how are they differ from normal languages like c, c++.
Any help is appreciated.
Thank you.
  1 comentario
Jan
Jan el 7 de Dic. de 2011
C code looks like someone has rolled an angry armadillo over the keyboard. I would not call it "normal".

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 7 de Dic. de 2011
In C and C++, an assertion taken usually exits the program completely. A message with the file name and line is printed out on standard error, and the abort() function is called, which sends a SIGABRT signal.
It is legal for a program to have installed a signal handler to catch SIGABRT, but in C the structure of signal handling makes it difficult for a containing routine to have a signal handler that resumes execution.
In C++, the same kind of message is printed by assert, and abort() is called, which raises SIGABRT, which it is possible to catch. However, "The program is terminated without executing destructors for objects of automatic or static storage duration, and without calling any atexit function" so assert() is a pretty hard failure mode in C++
In MATLAB, a formatted message is generated based upon further parameters, and an exception is generated. If I read the documentation correctly (but it is not clear), this exception can be caught through the try/catch mechanism. I also interpret (perhaps incorrectly) that any relevant object destructors will be executed, and that any relevant onCleanup() will be executed. If nothing catches the exception, the program will return to the MATLAB command prompt.
You said "normal languages like c, c++". Other "normal" languages may handle assertions through completely different mechanisms.
  2 comentarios
Mohan
Mohan el 7 de Dic. de 2011
Thank you very much for helping me out. But the answer raised one more question. Thatt is..
You have answered how it is different from c and c++ but, now I want to know the specialty of assertions in matlab, that feature of assertions is not available in the other languages.
Walter Roberson
Walter Roberson el 7 de Dic. de 2011
C++ has "exceptions", which are handled by try/catch and are triggered by "throw", but in C++, assert() falls outside that mechanism.
The MATLAB equivalent to C++'s "exceptions" is try/catch and usually triggered by "error" (or an internal equivalent). However, in MATLAB, assert() is basically just another error() that offers some fancy formatting.
I have confirmed that assert() can be handled by the standard try/catch mechanism.
>> try; assert(false,'man:from:mars','I only eat guitars'); catch me; disp(me); end
MException object with properties:
identifier: 'man:from:mars'
message: 'I only eat guitars'
stack: [0x1 struct]
cause: {}

Iniciar sesión para comentar.

Más respuestas (1)

Jan
Jan el 7 de Dic. de 2011
The command assert is explained in the documentation.
You can try these commands to find related topics in the help texts:
lookfor assert
docsearch assert

Categorías

Más información sobre Scope Variables and Generate Names en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by