Search code examples
matlabassert

Informative feedback from assert with matrices in Matlab


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.


Solution

  • 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 the testing framework.

    For example, your equality comparison would be implemented through verifyEqual:

    testCase = matlab.unittest.TestCase.forInteractiveUse;
    verifyEqual(testCase,A,B)