我可以抑制MATLAB中函数的命令窗口输出吗?

发布于 2025-01-30 13:13:23 字数 617 浏览 3 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

筱果果 2025-02-06 13:13:23

我相信您想要 evarc

从MATLAB文档(在八度中的函数相似):

使用“ evalc”功能重​​定向所有显示的输出
变量的命令窗口。这不会压制数字,但是
您是否允许您防止打印语句在
命令窗口。

例如:

[results, output] = evalc('mySubFunction(3)');

这仅显示在工作空间中存储以下变量:

>> [results, output] = evalc('mySubFunction(3)');
>> ans
ans = 6
>> output
output = 6
>> results
results = the input is: 3
the output is: 6

I believe you want evalc.

From the MATLAB-documentation (the function is similar in Octave):

Use the "evalc" function to redirect all the output displayed on the
command window to a variable. This will not suppress figures, but it
does allow you to prevent print statements from being displayed in the
command window.

As an example:

[results, output] = evalc('mySubFunction(3)');

This displays nothing but stores the following variables in the workspace:

>> [results, output] = evalc('mySubFunction(3)');
>> ans
ans = 6
>> output
output = 6
>> results
results = the input is: 3
the output is: 6
浅语花开 2025-02-06 13:13:23

就我而言,以任何语言来执行此操作的典型方法,而不仅仅是MATLAB / OCTAVE,是存在“ debug”或“冗长”变量,该变量存在于IF语句中,并决定是否打印这些变量日志。 IF语句可以直接在相关代码中使用,也可以包裹在负责执行日志记录的“漂亮”功能中。

该变量可以明确传递给该函数,也可以是一个全局变量。如果您打算直接从外壳运行脚本,而不是直接从MATLAB/八度环境中运行脚本,甚至可以从壳定义的环境变量中检测到它。或者,可以从配置文件中读取它。这完全取决于您的用例。


为了示例,这里是您的会话,使用全局变量方法如上所述。

%% in file mainScript.m 
global VERBOSE
VERBOSE = true;
data = 5;
if VERBOSE, disp('applying transformation...'); end
transformation = mySubFunction(data);
if VERBOSE, disp(['the result of the transformation is: ', num2str(transformation)]); end

%% in file mySubFunction.m 
function output = mySubFunction(input)
    global VERBOSE
    if VERBOSE, disp(['the input is: ', num2str(input)]); end
    output = input * 2;
    if VERBOSE, disp(['the output is: ', num2str(output)]); end
end

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.

%% in file mainScript.m 
global VERBOSE
VERBOSE = true;
data = 5;
if VERBOSE, disp('applying transformation...'); end
transformation = mySubFunction(data);
if VERBOSE, disp(['the result of the transformation is: ', num2str(transformation)]); end

%% in file mySubFunction.m 
function output = mySubFunction(input)
    global VERBOSE
    if VERBOSE, disp(['the input is: ', num2str(input)]); end
    output = input * 2;
    if VERBOSE, disp(['the output is: ', num2str(output)]); end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文