Question
I want it to 'listen' to the standard input stream in a running (compiled) Matlab executable.
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);
Answer
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.
Background
I have found how to do this in other languages, and here are some instructions for the compilation process.
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.
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.
What have I tried?
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.
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.
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.
Notes
- 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
function from a compiled MATLAB application and it didnt work. Is that it? – Conquistadorinput()
that requires hitting the "enter" key to submit inputs? – Klingel