力 c++程序显示“同步”使用 dos() 输出到 MATLAB 命令窗口
我正在 MATLAB 中使用 dos('myprog.exe')
执行我的 C++ 编译程序。 myprog 会生成一些输出,仅在 myprog.exe 完成执行后才会将其打印到 MATLAB 命令窗口。
有没有办法强制 MATLAB 在 myprog.exe 生成时而不是最后打印输出?
I'm executing my c++ compiled program in MATLAB with dos('myprog.exe')
. myprog produces some output that it is printed to the MATLAB command window only after myprog.exe finishes execution.
Is there a way to force MATLAB print the output when it is produced by myprog.exe and not at the end?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
回答
确保正确刷新 C++ 程序中的输出缓冲区。根据我的经验,有时在代码中插入额外的刷新命令(不仅仅是行尾命令)会有所帮助:
注意
您也可以尝试像这样调用您的程序:
matlab 帮助说:“'echo' 强制输出到命令行窗口,即使它也被分配到变量中。”
但是,这可能不起作用,因为(再次 matlab 帮助):
“控制台程序永远不会在后台执行。此外,MATLAB 软件始终等待 stdout 管道关闭后再继续执行。” 这意味着,matlab 可能会等到您的程序完成执行后才显示你的控制台输出。在这种情况下,你无能为力。
ANSWER
Make sure that you are flushing correctly the output buffers in your c++ program. In my experience it sometimes helps to insert additional flushing commands (not just end of lines commands) to your code:
NOTE
You might also try to call your program like this:
The matlab help says: "'echo' forces the output to the Command Window, even though it is also being assigned into a variable."
However this might not work because (again matlab help):
"Console programs never execute in the background. Also, the MATLAB software always waits for the stdout pipe to close before continuing execution. " This means, that matlab might wait until your program finishes its execution before it shows you the console output. In that case there's nothing you can do about it.