Assertions in matlab

조회 수: 3 (최근 30일)
Mohan
Mohan 2011년 12월 6일
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
Jan
Jan 2011년 12월 7일
C code looks like someone has rolled an angry armadillo over the keyboard. I would not call it "normal".

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2011년 12월 7일
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
Mohan
Mohan 2011년 12월 7일
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 2011년 12월 7일
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: {}

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2011년 12월 7일
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

카테고리

Help CenterFile Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by