Home > Back-end >  Informative feedback from assert with matrices in Matlab
Informative feedback from assert with matrices in Matlab

Time:01-11

Does there exist a simple function in Matlab that will give more informative feedback when using assert with matrices than the simple application of the assert function?

My simple application is:

>> assert(all([1 2; 3 4] == [1 2; 3 5], 'all'))
Assertion failed.

In Python, with numpy.testing.assert_equal the feedback from a failed assertion shows the two arrays.

I guess it would be possible to define further the arguments to the assert function errmsg, value1 and value2.

CodePudding user response:

assert is to validate intermediate values inside your code, so you get an error when something is not as you expect, and you can debug it. This is the “fail early” philosophy. You don’t need to get a detailed output here, it tells you that you need to break out the debugger.

Your use case seems closer to testing the output of a function, to verify it works as intended. This is a very different use case, for which MATLAB has enter image description here

Calling myfun with A=ones(4,4) and B=2*ones(4,4) leads to:

enter image description here

As I mentioned at the very beginning, the function above represent one of the possible implementations.

  • Related