如何以八度音程抑制命令的输出?
在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以修改
PAGER
变量(现在是一个函数)来重定向标准输出。在 Unix 系统上,您可以将其重定向到/dev/null
。在 Windows 上,我尝试简单地重定向到一个不执行任何操作的 Python 程序,并且它运行得很好。 (基本上,任何忽略输入的程序都可以)完成后您可以将其更改回来。也许可以将整个过程封装在一个函数中。
您还可以简单地以非交互方式运行脚本,并正常重定向输出。
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)You can just change it back after you're done. And maybe encapsulate this whole procedure in a function.
You can also simply run your scripts non-interactively, and redirect the output normally.
快速解决您的问题,也许甚至不值得一提的是重载
disp
函数,如下所示:然后,原始
disp
函数不会被调用,而是您的函数,其中没有输出生成的。我还尝试以某种方式重定向八度的 stdout,但不成功。我希望这个肮脏的解决方案可能足以满足您的情况^^
A quick hack of your problem and maybe not even worth mentioning is overloading the
disp
function like so: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^^这是一个非常古老的问题,但是我仍然遇到了同样的问题,这是可以提供帮助的技巧。可以使用
evalc
来包装有问题的函数调用。例如,您有一个代码:现在您可以这样做:
并使其保持沉默。
有趣的是,它甚至可以与内部的其他
eval
一起使用。我的意思是我们可以有:这是冗长的。现在:
这不是。
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:Now you can do it:
and make it silent.
Funny, but it even works with other
eval
inside. I mean we can have:which is verbose. Now:
and this is not.