Kiran
Kiran

Reputation: 8538

Print Line Number in Matlab

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

Answers (4)

Louis Gagnon
Louis Gagnon

Reputation: 139

To print the line number you can use this command:

printf(['Line number ' num2str(dbstack.line) '\n'])

Upvotes: 0

Biga
Biga

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

Shai
Shai

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

Harshal Pandya
Harshal Pandya

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

Related Questions