using matlab to send strings to the STDIN of another console application

24 次查看(过去 30 天)
I want to start a command line application using matlab. The application requires frequent user input (textual input via STDIN). After each input it writes output to file. If I start the application from within matlab (using the system command) I can use the matlab console to write input to the application, but since I would like to use the application in automatic testing, I would like to write to the input stream of my application automatically. I know I can start the application and use a file as input via the "<" operator in the command string, but my input strings depend on analysis of the last output. In other words, I do not know the next input string before analyzing the last output.
(I am using Matlab R2012b on a Windows 7 computer.)

采纳的回答

Friedrich
Friedrich 2013-4-17
编辑:Friedrich 2013-4-17
Sup :)
MATLAB itself don't has a feature for that but .NET has:
At least this works with the Windows shipped sort.exe so I guess it should also work with your exe:
function lh = sample()
% Initialize the process and its StartInfo properties.
% The sort command is a console application that
% reads and sorts text input.
sortProcess = System.Diagnostics.Process;
sortProcess.StartInfo.FileName = 'sort.exe';
sortProcess.EnableRaisingEvents = true;
sortProcess.StartInfo.CreateNoWindow = true;
% Set UseShellExecute to false for redirection.
sortProcess.StartInfo.UseShellExecute = false;
%Redirect the standard output of the sort command.
sortProcess.StartInfo.RedirectStandardOutput = true;
% Set our event handler to asynchronously read the sort output.
lh = sortProcess.addlistener('OutputDataReceived',@sortOutputHandler);
% Redirect standard input as well. This stream
% is used synchronously.
sortProcess.StartInfo.RedirectStandardInput =true;
% Start the process.
sortProcess.Start();
%Use a stream writer to synchronously write the sort input.
sortStreamWriter = sortProcess.StandardInput;
% Start the asynchronous read of the sort output stream.
sortProcess.BeginOutputReadLine();
%Prompt the user for 4 input text lines. Write each
%line to the redirected input stream of the sort command.
numInputLines = 0;
while(numInputLines ~= 4)
inputText = input('Enter a text line (or press the Enter key to stop):', 's');
numInputLines = numInputLines + 1;
if(~isempty(inputText))
sortStreamWriter.WriteLine(inputText);
end
end
disp('end of input stream');
%end the inputr stream to the sort command
sortStreamWriter.Close();
% wait for the sort process to write the sorted text lines
sortProcess.WaitForExit();
sortProcess.Close();
end
And you need:
function sortOutputHandler(obj,event)
%collect the sort command output and print in command window
if(~isempty(event.Data))
disp(event.Data);
end
end
So in your case you would need to post process the output of your exe in the sortOutputHandler and then write something to the STDIN using the same approach as in
Use a stream writer to synchronously write the sort input.
sortStreamWriter = sortProcess.StandardInput;
  2 个评论
João Ferreira
João Ferreira 2017-11-18
Thank you for the information. If, in addition, one wants to see the lines produced by the external console application as they are printed, read some values contained in those lines, and to perform some calculations and automatically pass some values to the external application when prompted by it, how could we do that?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Adding custom doc 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by