4.0

4.0 | 1 rating Rate this file 5 Downloads (last 30 days) File Size: 1.28 KB File ID: #40415

Keyboard replacement with stack trace

by

 

20 Feb 2013 (Updated )

Display a stack trace showing where keyboard was used

| Watch this File

File Information
Description

The 'keyboard' command is great for debugging, but if you have multiple instances of keyboard, it is very difficult to tell which one is being called. This complicates debugging somewhat.

kdb() is a drop-in replacement for keyboard, and it shows a stack trace, preview of the next line to be executed, and a hyperlink to that line of code. For example, if foo() calls bar() calls baz() and we have a 'keyboard' command in baz(), the user sees

>> foo
K>>

whereas with kdb() the user sees

>> foo

baz:2 << bar:2 << foo:3
3 y = x+2;
K>>

The '3' is a hyperlink to line 3 of baz.m, and depending on settings the Matlab editor may automatically open to the appropriate kdb() statement. The only caveat is that unlike keyboard, kdb() cannot be the last statement in the function.

Required Products MATLAB
MATLAB release MATLAB 7.13 (R2011b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (2)
20 Feb 2013 Romesh

Thanks for the suggestion, I am also a big fan of eliminating calls to eval() wherever possible. I've submitted an updated version incorporating both your changes

20 Feb 2013 per isakson

Your idea is very good. I've never been comfortable with KEYBOARD. To me your function provides a "persistent" break-point. However, I have modified the code to handle functions in packages.

stk = dbstack('-completenames');
dbstop( 'in', stk(2).file, 'at', num2str( stk(2).line+1 ) )

Updates
20 Feb 2013

Updated with suggestions from Per Isakson to support packages

Contact us