我可以抑制MATLAB中函数的命令窗口输出吗?
在MATLAB中,我经常在脚本中使用函数,以一种或几种方式将某些内容输出到命令窗口,例如:
function output = mySubFunction(input)
disp(['the input is: ', num2str(input)])
output = input * 2;
disp(['the output is: ', num2str(output)])
end
如果我现在在脚本中使用此功能,它看起来像这样:
data = 5;
disp('applying transformation...')
transformation = mySubFunction(data);
disp(['the result of the transformation is: ', num2str(transformation)])
这当然是一个非常简单的例如,实际上,功能通常非常复杂,并且会产生很多输出。在这种情况下,为了更好地概述使用命令窗口中的主脚本中发生的情况,我想抑制mySubluction中的disp()函数。有什么方法可以简单地从我的主要脚本中做到这一点(也就是说,不潜入可能非常复杂的功能和子功能来评论所有disp()函数或插入半龙)?
In Matlab I often use functions in my script that output something to the command window in one or several ways, for instance like this:
function output = mySubFunction(input)
disp(['the input is: ', num2str(input)])
output = input * 2;
disp(['the output is: ', num2str(output)])
end
If I now use this function in my script it would look something like this:
data = 5;
disp('applying transformation...')
transformation = mySubFunction(data);
disp(['the result of the transformation is: ', num2str(transformation)])
This is of course a very simple example, in fact the functions are often quite complicated and give a lot of output. In this case, to keep a better overview of what is going on in my main script using my command window, I would like to suppress the disp() functions in my mySubFunction. Is there any way that I can do this simply from my main script (that is, without diving into possibly very complicated functions and subfunctions to comment out all disp() functions or insert semicolons)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您想要
evarc
。从MATLAB文档(在八度中的函数相似):
例如:
这仅显示在工作空间中存储以下变量:
I believe you want
evalc
.From the MATLAB-documentation (the function is similar in Octave):
As an example:
This displays nothing but stores the following variables in the workspace:
就我而言,以任何语言来执行此操作的典型方法,而不仅仅是MATLAB / OCTAVE,是存在“ debug”或“冗长”变量,该变量存在于IF语句中,并决定是否打印这些变量日志。 IF语句可以直接在相关代码中使用,也可以包裹在负责执行日志记录的“漂亮”功能中。
该变量可以明确传递给该函数,也可以是一个全局变量。如果您打算直接从外壳运行脚本,而不是直接从MATLAB/八度环境中运行脚本,甚至可以从壳定义的环境变量中检测到它。或者,可以从配置文件中读取它。这完全取决于您的用例。
为了示例,这里是您的会话,使用全局变量方法如上所述。
The typical way to do this in any language as far as I'm concerned, not just matlab / octave, is to have a 'debug' or 'verbose' variable present, which is checked against an if statement and decides whether to print those logs. The if statement could be used in the relevant code directly, or wrapped inside a 'prettier' function responsible for doing the logging.
This variable may be passed explicitly to the function, or it could be a global variable. If you plan to run your script directly from the shell rather than directly from a matlab/octave environment, it could even be detected from from a shell-defined environmental variable. Alternatively, it could be read from a config file. It all depends on your use case.
For the sake of an example, here is your session, transformed as above, using the global variable approach.