Each arithmetic operator in Matlab has an associated method that is called when this operator is called. For example, the method corresponding to * (matrix multiplication) is called mtimes .
For each operator, you can define a method for variables of type double , which shadow is an inline method and changes its behavior: in your case, enable custom error checking, and then call the inline method.
The advantages of this approach are as follows:
No changes are required in your code : you usually use * , *. , + , etc .; but their behavior (error checking) will change.
When you think that you have done debugging, you only need to remove your own methods from this path. This will restore normal behavior and thus avoid a speed penalty . Later, if you need to debug again, all you have to do is return the modified methods to the path.
In the following example, I use * and the associated mtimes . Your new mtimes method should be placed in the Matlab path in the appropriate folder so that it takes precedence over bulitin mtimes . This means that the folder must be in the matlab path . Or you can use the current folder, which takes precedence over everyone else.
Within the selected folder, create a folder named @double , and in it create a file called mtimes.m . This tells Matlab that your mtimes.m file should be used whenever * is called with double entries.
The contents of mtimes.m will consist of the following lines:
function C = mtimes(A,B) if ndims(A)~=2 || ndims(B)~=2 %// number of dimensions is incorrect error('MATLAB:mtimes_modified:ndims', ... 'One of the arrays is not a matrix: numbers of dimensions are %i and %i',... ndims(A), ndims(B)); elseif max(size(A))>1 & max(size(B))>1 size(A,2)~=size(B,1) %// dimensions are not appropriate for matrix multiplication, %// or for multiplying by a matrix by a scalar error('MATLAB:plus_modified:dimagree',... 'Sizes do not match: %ix %i and %ix %i', ... size(A,1), size(A,2), size(B,1), size(B,2)); else C = builtin('mtimes', A, B); %// call actual mtimes to multiply matrices end
Examples of results:
>> rand(3,4,5)*rand(6,7) Error using * (line 3) One of the arrays is not a matrix: numbers of dimensions are 3 and 2 >> rand(3,4)*rand(2,5) Error using * (line 7) Sizes do not match: 3 x 4 and 2 x 5 >> rand(3,4)*rand(4,2) ans = 0.3162 0.3009 1.2628 0.7552 1.2488 0.8559