Interface console application asynchronously
4 次查看(过去 30 天)
显示 更早的评论
This question probably has been answerd a lot but I cant seem to find an answer to it.
I have an console application 'evolver64.exe' which provides a command line interface when launched from the command line. I wish to launch and interact with it semi-asynchronously. For example:
[programHandle, programOutput] = launchProgram('evolver64.exe'); % Launches program and captures initial output.
programOutput2 = executeOnProgram(programHandle, 'my string command'); % Executes the specified string on the command.
programOutput3 = endProgram(programHandle); % Ends program capturing final output.
Where 'programOutput#' are strings containing the programs's new output before stopping on user input.
All functions only return when the program goes back to waiting for user input. Is this functionality possible?
My goal is to automate the usage of this program through Matlab to run complex tasks.
0 个评论
回答(1 个)
Ayush
2023-9-4
Yes, it is possible to achieve the functionality you described by using MATLAB's `system` function and some additional code.
function [programHandle, programOutput] = launchProgram(programPath)
% Launch the program and capture initial output
command = [programPath ' &'];
[programHandle, ~] = system(command);
pause(1); % Wait for the program to initialize
programOutput = executeOnProgram(programHandle, ''); % Capture initial output
end
function programOutput = executeOnProgram(programHandle, command)
% Execute the specified command on the program
[~, programOutput] = system(['echo ' command ' | tee /dev/fd/' num2str(programHandle)]);
pause(1); % Wait for the program to process the command
end
function programOutput = endProgram(programHandle)
% End the program and capture final output
programOutput = executeOnProgram(programHandle, 'exit'); % Send exit command
pause(1); % Wait for the program to exit gracefully
end
Here's how you can use these functions to automate the usage of the program:
programPath = 'evolver64.exe';
[programHandle, programOutput] = launchProgram(programPath);
disp(programOutput); % Display initial output
command = 'my string command';
programOutput2 = executeOnProgram(programHandle, command);
disp(programOutput2); % Display output after executing the command
programOutput3 = endProgram(programHandle);
disp(programOutput3); % Display final output
In this example, the `launchProgram` function launches the program in the background using the `system` function with the `&` operator. It captures the initial output by executing an empty command.
The `executeOnProgram` function sends a command to the program by echoing the command and redirecting it to the file descriptor of the program's input. It captures the output by using the `system` function with the `tee` command.
The `endProgram` function sends an exit command to the program using the `executeOnProgram` function. It captures the final output before the program exits.
Please note that the `pause` function is used to allow the program some time to process the commands and produce output. You may need to adjust the duration of the pause depending on the specific program and its response time.
Hope it helps!
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!