When to use assert () in matlab?

Since Matlab is interpreted, they usually spend a lot of time at the beginning of a function that provides a function signature. for instance

if nargin ~= 2; error('must provide two input args a and b'); end if a < 0||a ~=floor(a); error('input arg1 must be positive non-zero integer'); end if ~isa(b,'cell') ... 

Is it better to use Matlab assert () instead? If not, when is the appropriate time to use assert () in Matlab?

There is a lot of discussion about using assert in production code here , but I'm not sure if this applies to interpreted code. Likewise, another good discussion here , and I agree with @Dan Dyer regarding the statement to express confidence in the current state. However, looking at a similar discussion for Python here , people say, use only assert for situations that should never happen (for example, exceptions for exceptional cases), which is a little contradictory to wrt previous links.

Maybe this is more a question of how assert plays in interpreted languages ​​and less about Matlab.

+5
source share
3 answers

For the most part, there is no difference between

 assert(X,...) 

and

 if (~X) error(...) end 

and your choice between them is a matter of convenience or style.

The difference between non-production and production code in MATLAB projects often does not coincide with the difference in projects based on other languages.

This is partly because, as you say, MATLAB is usually interpreted, not compiled; although it is possible to create applications using MATLAB Compiler or Builder, which, although not strictly “compiled,” do not have visible source code and cannot be debugged. For such applications, you will need to handle exceptions and errors as carefully as with a compiled language.

This is also partly because “production” often means something different for projects that use MATLAB than for projects in other languages; for example, this may mean that the MATLAB code is automatically converted to C for deployment on a car engine controller, or it may mean that some MATLAB code ran a financial forecasting model and wrote the results to a production database.

There is a special case where assert should be used instead of if..error..end , which is used when you use MATLAB Coder to generate C code from MATLAB code. MATLAB Coder checks the assert in MATLAB code to determine the properties of the variables that it needs to convert to C, and can generate more efficient C code if it can accept facts about the variables you state (for example, sizes and types of arrays).

One last point: for the specific activity that you are mentioning, I would not use a single method for enforcing the function signature - inputParser , as a rule, is more reliable and consistent (albeit a little detailed), but, more importantly, it encourages Design you first.

+3
source

The way MATLAB handles assertions means that, from the user's point of view, there is no difference between assertions:

 if error_check == false error('function:state','Error message'); end 

and

 assert(error_check==false,'function:state','Error message'); 

Both results lead to the same result that is displayed to the user, and the same data is stored in lasterr . Both are captured by a try-catch . Based on a very quick and dirty test, I would presumably say that assert is ~ 5% slower than error (although in reality this doesn’t really affect the big scheme of things). As a result, the only real difference is the programmer / maintainer code, which makes it a largely stylistic choice.

Both assert and error have their pros and cons. assert looks a little cleaner and takes up less space, but not necessarily, as is obvious what happens at first glance. For my personal use, I would prefer the error case, because I can put a breakpoint in the line with the error call, and it will only break there if the error is about to be selected - this cannot be done as easily as with the assert message (you can use dbstop in file if error , but this potentially has its own problems). I can also insert debug code in the if-statement to print information about the state of the program before the error is reset.

+2
source

Another perspective for this problem is uptime. Usually you expect your code to work without errors. This means that you do not care about the milliseconds that Matlab takes to generate an error, but only for a logical test.

here is the code i used for comparison:

 function o=test a = 2; o = [0 0]; tic; if a~=2 error('a is not 2') end o(1) = toc; tic assert(a==2,'a is not 2') o(2) = toc; 

Now, run this function enough time to get good statistics:

 for i=1:10000 o(i,:) = test; end mean(o) 

and results:

ans =

1.0e-05 *

0.0088 0.3548

bottom line:

assert is much slower than if-else.

+2
source

Source: https://habr.com/ru/post/1207767/


All Articles