Reading from comand prompt executables

5 views (last 30 days)
Evan
Evan on 5 Apr 2011
I have two versions of an executable that run in windows cmd interface. Each executable connects to a different device and continuously reports information back from the device. The output in the cmd window would be the line ""X = 5.3 Y = 5.3 Z = 3.4 ButtonState = 0x0"" And every few milliseconds, it will write a new line with the new value from the device. I want to be able to read into MATLAB the last line from the cmd window from the two separate cmd windows so that I can convert them and display them in a MATLAB gui.
Is there a way to read from two windows cmd (without saving the output) in close to real time?
All help is appreciated! Thank you, Evan

Answers (1)

Richard Alcock
Richard Alcock on 5 Apr 2011
I think you will have to look at using some Java classes to do this. The ProcessBuilder and Process classes will enable you to do this. Here's an example that calls the Windows "ping.exe" utility.
pingCmd = {'ping.exe', '-n', '10', 'www.google.com'};
processBuilder = java.lang.ProcessBuilder(pingCmd);
pingProcess = processBuilder.start();
% Set up a reader to read the output from the
% ping process
reader = ...
java.io.BufferedReader(...
java.io.InputStreamReader(...
pingProcess.getInputStream() ...
) ...
);
% Loop until there is some output
nextLine = char( reader.readLine );
while isempty(nextLine)
nextLine = char( reader.readLine );
end
% Then loop until there is no more output
while ~isempty(nextLine);
fprintf('PING output: %s\n', nextLine);
nextLine = char( reader.readLine );
end
% Get the exit value of the process
exitValue = pingProcess.exitValue
  1 Comment
Bill Comisky
Bill Comisky on 16 Jan 2013
The read loop works a little better if you check for null return from readLine which is an empty double matrix in matlab. Otherwise it returns a java string.. this way you don't quit too early if your output has empty lines in it:
while true
line = input.readLine;
if isnumeric(line) && isempty(line)
% java null is empty double in matlab
break;
end
% do stuff with char(line) here
end

Sign in to comment.

Categories

Find more on Java Package Integration in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!