Matlab's cryptic error messages

May 17, 2014
Sometimes MATLAB's error messages can be very cryptic and misleading. Following are examples of error messages which I encountered while trying to solve \(\mathbf{A}x=b\) in least square sense using MATLAB's lsqr function. %% Error 1 Error using iterapp (line 60) user supplied function ==> @(x,transp_flag)transform_X(x,transp_flag, ...) failed with the following error: Not enough input arguments. Error in lsqr (line 180) u = u - iterapp('mtimes',afun,atype,afcnstr,x,varargin{:},'notransp'); %% Error 2 Error using * MTIMES is not supported for one sparse input and one single input. Error in iterapp (line 29) y = afun * x; Error in lsqr (line 180) u = u - iterapp('mtimes',afun,atype,afcnstr,x,varargin{:},'notransp'); For an experienced MATLAB user, error 2 may be easier to locate because of the message: "Error using *". However, when I was actually working, I got error 1 which said "Not enough input arguments". It took me sometime to figure out that both the errors were related to mismatching datatypes in input parameters. Both the situations were actually identical, except that in first case I was using function handle Afun in place of matrix representation of \(\mathbf{A}\). Here are the actual statements which raised errors: x = lsqr(Afun, b, 1e-9, 100, [], [], x0); % Raises Error 1 x = lsqr(A, b, 1e-9, 100, [], [], x0); % Raises Error 2
And here is the code snippet for Error 2 above. The code can be modified easily to create function handle Afun (see Example 2 here). Notice the statement x0 = single(rand(size(b))*5); in the code snippet which changes the datatype and causes error. n = 50; on = ones(n,1); A = spdiags([-2*on 4*on -on],-1:1,n,n); b = sum(A,2); x0 = single(rand(size(b))*5); x = lsqr(A,b,1e-9,100,[],[],x0);
The take-away message: Always check datatypes when MATLAB's error messages don't make sense!
Read more ...