Matlab命令窗口输出源 [英] Matlab Command Window Output Source
问题描述
可能重复:
在Matlab中查找当前m文件的位置
Possible Duplicate:
Find location of current m-file in Matlab
有人知道如何找到哪个函数向Maltab中的命令窗口提供输出吗?我已经编写了具有许多功能的代码,已经将输出输出到命令窗口,但是我找不到负责该功能的函数.谢谢!
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 !
推荐答案
如果所有输出均由您自己的代码打印,则可以轻松地将所有fprintf
和disp
调用替换为您自己的函数调用,这些函数调用可以为所有输出加上前缀函数名称.
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.
代码如下:
getfunctionname.m:
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:</p>
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:
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
这篇关于Matlab命令窗口输出源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!