-1

Possible Duplicate:
Find location of current m-file in Matlab

does anyone knows how to find which function is providing output to the command window in Maltab? I've written a code with many functions, I have output to the command window but I can't find which function is responsible for that. Thanks !

Community
  • 1
  • 1

1 Answers1

0

If all output is printed by your own code, you can easily replace all fprintf and disp calls with your own function calls that optionally prefix all output with the function name.

Here's the code:

getfunctionname.m:

function [CurrentFunctionName, PreviousFunctionName] = getfunctionname()
CurrentFunctionName = '';
PreviousFunctionName = '';
MyStack = dbstack('-completenames');
if (length(MyStack) < 2)
    error('Function getfunctionname.m cannot be called from MATLAB console.');
elseif (length(MyStack) == 2)
    CurrentFunctionName = MyStack(2).name;
else
    CurrentFunctionName = MyStack(2).name;
    PreviousFunctionName = MyStack(3).name;
end
return

myprintf.m:

function myprintf(varargin)
global PrefixOutputWithFunctionName
if (PrefixOutputWithFunctionName)
    [~, PreviousFunctionName] = getfunctionname;
    fprintf('### %s.m:\n', PreviousFunctionName);
end
disp(sprintf(varargin{:}));
return

mydisp.m:

function mydisp(varargin)
global PrefixOutputWithFunctionName
if (PrefixOutputWithFunctionName)
    [~, PreviousFunctionName] = getfunctionname;
    fprintf('### %s.m:\n', PreviousFunctionName);
end
disp(varargin{:});
return

mainfunction.m:

function mainfunction()
global PrefixOutputWithFunctionName

% set PrefixOutputWithFunctionName to false to disable prefixing.
PrefixOutputWithFunctionName = true;

% the code goes here...

% example output.
myprintf('some text...\n some more text...');
return
nrz
  • 10,435
  • 4
  • 39
  • 71
  • Thanks for the reply ! I couldn't get it... I tried but I get the following error: ??? Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N) to change the limit. Be aware that exceeding your available stack space can crash MATLAB and/or your computer. Error in ==> getfunctionname – Martin Saravia Jun 14 '12 at 19:22
  • Well, `getfunctionname` uses `dbstack` to get the name of the function that called `getfunctionname` function and returns it. Actually you could also use `mfilename` for that, but to get the name of previous function you need to used `dbstack` anyway. `myprintf` checks the value of global variable `PrefixOutputWithFunctionName`, if it's true then it prefixes the `fprintf` output with the name of the function that called it. `mainfunction.m` is just a simple example how you could define the global variable `PrefixOutputWithFunctionName` and use `myprintf`. `mydisp` is similar but for `disp`. – nrz Jun 14 '12 at 21:24
  • `getfunctionname` runs nicely for me by running `mainfunction` example code. I think you are using it in a different configuration (maybe inside a recursive function?) and somehow then you run out of stack or reach the recursion limit, maybe `dbstack` has something to do with it, I'm not sure. – nrz Jun 14 '12 at 21:48
  • Thanks again nrz, I'm not sure whats going on. It also works for me with mainfunction but when I use it inside the full code it reaches the recursion limit. I'll watch it again and let you know ! – Martin Saravia Jun 14 '12 at 23:02