MATLAB: Generate an error that refers to the line where the error occurred.

errorerror generating

Greetings,
I am currently in the process of adding error messages to my code. However, an issue that I cannot seem to solve is how to make my error message point to where the error occurred, and not the line where the 'error' command is placed.
E.g. the problem occurs at line 72, where the value x0 is wrong, but the check and the error message is placed at lines 106-108, as shown below
70 - % Location of the scatterer.
71 - z0 = 380;
72 - x0 = 210;
.
.
.
106 - if xsc(1) < 0 || xsc(end) > D
107 - error('Scatterer out of bounds. Redefine cross-range position.')
108 - end
Therefore, if the error message is triggered, I get the output
Error using IKMt (line 107)
Scatterer out of bounds. Redefine cross-range position.
Instead, I would like the error message to link to line 72, where x0 is inserted improperly.
Currently running Matlab R2012b.
Any help would be greatly appreciated.

Best Answer

The source code is the database already to solve the problem. Because Matlab is an interpreted language, you can work in the source code directly and find the line containing the definition of the value manually. In opposite to this, compiled languages like C do not allow this directly with the executable. Therefore using Matlab's editor to examine the source code is the solution already.
Of course you can improve the error checking manually:
% MAGIC_STRING:&$/(
a = 10;
...
if a < 0 || a > D
magicError('Bad variable', mfilename('fullpath'), '&$/(')
end
and the magic error handler:
function magicError(Msg, File, Magic)
FileCont = strsplit(fileread(File), '\n');
Line = find(~cellfun('isempty', strfind(FileCont, Magic)), 1);
Cmd = sprintf('opentoline(''%s'', %.3d)', File, Line);
HRefStr = ['<a href="matlab:', Cmd, '">Edit definition</a>'];
fprintf(2, '*** Edit definition: %s\n', HRefStr);
error(Msg);
end
If this is really useful is a question of taste. I do not like such meta-programming, because Matlab's IDE offers everything which is needed already. You can search for the definition of a variable in the source code conveniently and reliably in the editor instead. Note that a typo in the magic key will confuse the system already. The same can happen, if any other programmer ignores this mechanism and inserts new definitions anywhere else. Then this magic system pretends to be smart and safe, but the occurring messages are misleading.
It is useful to separate data, code and GUIs strictly. If the shown parameters are variables, better move them to a dedicated function or MAT file. Do not let a user edit the source codes of your functions, because this is prone to typos. In the productive phase of a program, the source codes should be write-protected (or compiled) to prevent any unwanted editing.
As soon, as the the definition of the parameter is moved out of the productive code, your problem will vanish. Then:
Param = DefineParams();
if Param.a < 0 || Param.a > D
error('Bad definition of "a", please edit: %s', ...
which('DefineParams.m'));
end
Related Question