Keyboard replacement with stack trace

by

 

20 Feb 2013 (Updated )

Display a stack trace showing where keyboard was used

kdb
function kdb
    % Keyboard debug caller 
    % Provides more information including the stack trace than simply using 
    % 'keyboard'
    % Use kdb() directly instead of keyboard()
    % Note this function will not work if it is the last line in the program
    %
    % By Romesh Abeysuriya 15-11-12
    % Updated with suggestions from Per Isakson 21-2-13
    
    s = dbstack('-completenames');
    fprintf(1,'\n')
    for j = 2:length(s)
        fprintf('%s:%i << ',s(j).name,s(j).line);
    end
    fprintf('\b\b\b\b\n');
    dbstop('in',s(2).file,'at',num2str(s(2).line-1));

Contact us