Reputation: 8538
I have a Matlab function which runs into few thousands of lines of code. Under certain condition, it is breaking. I can as well, debug the code and run step-by-step.
So, I have try, catch block in Matlab to handle the error. In addition to this, is it possible to capture, the line number of the code as well.
For Example :
try
Error here <-----
catch err
disp(['Error occured on line No ' num2str(lineNo])
end
Any idea, how it can be implemented ?
Upvotes: 2
Views: 2634
Reputation: 139
To print the line number you can use this command:
printf(['Line number ' num2str(dbstack.line) '\n'])
Upvotes: 0
Reputation: 37
You can try in this way:
try
Error here <--------------
catch err
disp([err.identifier]);
disp([err.message]);
for e=1:length(err.stack)
disp(['Error in ' err.stack(e).file ' at line ' num2str(err.stack(e).line)]);
end
end
Upvotes: 0
Reputation: 114816
You may also consider using
>> dbstop if error
before running the code: this way when an error occurs, Matlab creates a debug breakpoint and allow you to debug at the error.
Upvotes: 2
Reputation: 1624
Try this. This will print out the line numbers along with the full stack.
try
%some code;
catch exc
getReport(exc, 'extended')
end
Upvotes: 4