How to send commands to command window programmatically in MATLAB?
Not using the !
command or system()
. Those are "one and done" functions.
But you can use Java's java.lang.Process
API from within Matlab to control and interact with an ongoing process.
function control_another_processpb = java.lang.ProcessBuilder(["myCommand", "myArg1", "myArg2"]);proc = pb.start; % now proc is a java.lang.Process objectstdin = proc.getOutputStream; % Here's how you send commands to the processstdout = proc.getInputStream; % And here's how you get its outputstderr = proc.getErrorStream;% ... now do stuff with the process ...end
You can use this with a shell, with python
, or any other command.
Here's a Matlab class that wraps up the Java code to make it convenient to work with in Matlab: https://github.com/apjanke/janklab/blob/master/Mcode/classes/%2Bjl/%2Butil/Process.m