如何以八度音程抑制命令的输出?

发布于 2024-12-17 21:20:36 字数 407 浏览 3 评论 0原文

在 Octave 中,我可以抑制或隐藏在行尾添加分号的指令的输出:

octave:1> exp([0 1])
ans = [ 1.0000   2.7183 ]
octave:2> exp([0 1]);
octave:3> 

现在,如果函数显示文本(例如使用 disp() 或 < code>print()) 在返回其值之前?换句话说,我希望能够做到这一点:

disp("Starting...");
% hide text the may get displayed after this point
% ...
% show all text again after this point
disp("Done!");

In Octave I can suppress or hide the output of an instruction adding a semicolon to the end of a line:

octave:1> exp([0 1])
ans = [ 1.0000   2.7183 ]
octave:2> exp([0 1]);
octave:3> 

Now, how can I suppress the output if the function displays text (e.g. using disp() or print()) before returning its value? In other words, I want to be able to do this:

disp("Starting...");
% hide text the may get displayed after this point
% ...
% show all text again after this point
disp("Done!");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

空宴 2024-12-24 21:20:36

您可以修改 PAGER 变量(现在是一个函数)来重定向标准输出。在 Unix 系统上,您可以将其重定向到 /dev/null。在 Windows 上,我尝试简单地重定向到一个不执行任何操作的 Python 程序,并且它运行得很好。 (基本上,任何忽略输入的程序都可以)

PAGER('/dev/null');
page_screen_output(1);
page_output_immediately(1);

完成后您可以将其更改回来。也许可以将整个过程封装在一个函数中。

oldpager = PAGER('/dev/null');
oldpso = page_screen_output(1);
oldpoi = page_output_immediately(1);

% Call function here

PAGER(oldpager);
page_screen_output(oldpso);
page_output_immediately(oldpoi);

您还可以简单地以非交互方式运行脚本,并正常重定向输出。

octave script.m > /dev/null

You can modify the PAGER variable (which is now a function) to redirect standard output. On Unix systems, you can redirect it to /dev/null. On Windows, I tried simply redirecting to a Python program that does nothing, and it works decently. (Basically, any program that ignores the input will do)

PAGER('/dev/null');
page_screen_output(1);
page_output_immediately(1);

You can just change it back after you're done. And maybe encapsulate this whole procedure in a function.

oldpager = PAGER('/dev/null');
oldpso = page_screen_output(1);
oldpoi = page_output_immediately(1);

% Call function here

PAGER(oldpager);
page_screen_output(oldpso);
page_output_immediately(oldpoi);

You can also simply run your scripts non-interactively, and redirect the output normally.

octave script.m > /dev/null
乜一 2024-12-24 21:20:36

快速解决您的问题,也许甚至不值得一提的是重载 disp 函数,如下所示:

function disp(x)
end

然后,原始 disp 函数不会被调用,而是您的函数,其中没有输出生成的。

我还尝试以某种方式重定向八度的 stdout,但不成功。我希望这个肮脏的解决方案可能足以满足您的情况^^

A quick hack of your problem and maybe not even worth mentioning is overloading the disp function like so:

function disp(x)
end

Then the original disp function is not called but yours instead in which no output is generated.

I also tried to somehow redirect stdout of octave, but unsuccessful. I hope that this dirty solution maybe will suffice in your situation^^

猫瑾少女 2024-12-24 21:20:36

这是一个非常古老的问题,但是我仍然遇到了同样的问题,这是可以提供帮助的技巧。可以使用 evalc 来包装有问题的函数调用。例如,您有一个代码:

[a, b] = verbose_func(x,y);

现在您可以这样做:

evalc('[a, b] = verbose_func(x,y)');

并使其保持沉默。

有趣的是,它甚至可以与内部的其他 eval 一起使用。我的意思是我们可以有:

code_str = '[a, b] = verbose_func(x,y)';
eval(code_str);

这是冗长的。现在:

code_str = '[a, b] = verbose_func(x,y)';
evalc('eval(code_str)');

这不是。

It's a very old question, but still, I've encountered the same problem and here is the trick that can help. One can use evalc to wrap a problematic function call. E.g. you have a code:

[a, b] = verbose_func(x,y);

Now you can do it:

evalc('[a, b] = verbose_func(x,y)');

and make it silent.

Funny, but it even works with other eval inside. I mean we can have:

code_str = '[a, b] = verbose_func(x,y)';
eval(code_str);

which is verbose. Now:

code_str = '[a, b] = verbose_func(x,y)';
evalc('eval(code_str)');

and this is not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文