在Matlab exe中使用标准io stream:stdin和stdout [英] Using standard io stream:stdin and stdout in a matlab exe
问题描述
问题
我希望它在正在运行(已编译)的Matlab可执行文件中监听"标准输入流.
I want it to 'listen' to the standard input stream in a running (compiled) Matlab executable.
我相信这是用c
或类似语言完成的方式:
This is how I believe it is done in c
or a similar language:
#include stdio.h
fgets(line, 256, stdin)
或更详细地说,它可以这样使用:
Or more elaborately, it it can be used as such:
if (!fgets(line, 256, stdin))
return;
if (line[0] == '\n')
continue;
sscanf(line, "%s", command);
答案
为了完整起见,我将完整保留背景和注释,但是在Amro和EitanT的帮助下,我设法将其解决了.
For completeness I will leave the background and notes intact, but with the help of Amro and EitanT I have managed to work it out.
背景
我已经找到了其他语言的用法,并且这里是编译过程的一些说明.
I have found how to do this in other languages, and here are some instructions for the compilation process.
但是,我在任何地方都没有找到如何聆听" Matlab中的输入的方法. 我最接近的是此说明 Octave中类似于C的IO,但是在MATLAB中寻找解决方案时,我无法在这方面取得进展.
However, I have not found anywhere how to 'listen' to the input in Matlab. The closest I have come is this description of C-like IO in Octave, but I cannot make progress with this as I looking for a solution in MATLAB.
请注意,无法更改或包装通过流发送数据的程序,并且我更喜欢纯MATLAB解决方案,而不是包装整个程序.如果我要用另一种语言从MATLAB调用一个琐碎的函数,那没关系.
Note that altering or wrapping the program that sends the data over the stream is not possible, and that I would prefer a pure MATLAB solution rather than wrapping my entire program. If I were to call a trivial function from MATLAB in a different language that would be ok.
我尝试了什么?
我尝试了在命令窗口中尝试了一些功能,例如fgets(0)
(fid = 0
似乎是与stdin
相对应的ID(如@EitanT所述,在尝试fopen(0)
)),但它只会返回:
I tried a few functions from the command window like fgets(0)
(fid = 0
seems to be the id corresponding to stdin
(as mentioned by @EitanT and seen when trying fopen(0)
) )but it just returns:
Operation is not implemented for requested file identifier.
我还考虑过使用MATLAB中的选项来调用系统命令或执行java
/perl
命令,但是到目前为止还算不上运气.我也不确定这些在编译后是否仍然有效.
I have also considered using the option in MATLAB to invoke system commands or execute java
/ perl
commands, but so far without luck. I am also not sure whether these would still work after compilation.
此外,当我通过cmd
打开程序时,我尝试使用input('prompt','s')
可以正常工作,但是在按Enter键之前什么也没做. (当然,我所听的程序永远不会执行,在最佳情况下,我可以在每一行的末尾得到\n
.)
Furthermore I attempted to use input('prompt','s')
this works when I open the program via cmd
, but does not do anything until I hit enter. (Which the program that I listen to of course will never do, in the best case I can get \n
at the end of each line).
I also tried out waitinput
from File Exchange but I think this is a dead end as it did not catch anything and seems to perform quite poorly.
注释
- 我正在使用Windows 7和MATLAB 2012b.
- 我在文件交换上找到了
popen
,但是该功能似乎不可用对于Windows. - 当我只键入
'show me'
之类的内容时,它会正确发送到标准输出流.
- I am using Windows 7 and MATLAB 2012b.
- I found
popen
on File Exchange but that does not seem to be available for Windows. - When I simply type something like
'show me'
this is properly sent to the standard output stream.
推荐答案
事实证明,input
读取标准输入流.
It turns out that input
reads the standard input stream.
之所以无法收集我的输入,是因为我按如下方式使用它:
The reason why I failed to collect my inputs, is because I was using it as follows:
input('prompt','s')
结果将字符串'prompt'
发送到了调用我的应用程序的程序,并且由于它认为这是无效的响应/请求,因此未发送任何内容.
As a result the string 'prompt'
was sent to the program calling my application, and as it considered this an invalid response/request it did not send anything.
我已经成功制作了一个小型测试程序,与我之前怀疑的不同,发送命令后其他应用程序不会按Enter键不是问题.
I have succeeded in making a small test program, and unlike I suspected before it is NOT a problem that the other application doesn't hit enter after sending a command.
一般解决方案
这是我当前设置的方式,
This is the way I have my current setup,
while 1
stdin = input('','s'); % Note the empty first argument
if ~isempty(stdin)
stdout = process_input(stdin);
stdout % Displaying the result (And thus sending it to stdout)
end
end
这篇关于在Matlab exe中使用标准io stream:stdin和stdout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!