1

I'm trying to use use Matlab to:

  1. Create a new Java process to spawn a 2nd instance of Matlab and then

  2. Write a command to that 2nd instance of Matlab from the first instance of Matlab.

The code seems to run fine, but I don't see anything appear in the command window of the 2nd Matlab instance. What am I doing wrong?

The code I've tried is:

% Start a 2nd instance of Matlab
MatlabProcess = java.lang.Runtime.getRuntime().exec('matlab -nosplash');
pause(20); % I don't know if this pause is really needed.

% Set up a buffered Java stream writer to write to the new Matlab Process
OutputStream            = MatlabProcess.getOutputStream();
OutputStreamWriter      = java.io.OutputStreamWriter(OutputStream);
OutputBufferedWriter    = java.io.BufferedWriter(OutputStreamWriter);

% Write some text to the 2nd Matlab Instance
OutputBufferedWriter.write('pi\n');
OutputBufferedWriter.flush();
0

1 Answer 1

1

I believe you get either a command window, or use of stdin/stdout, not both, depending on whether the matlab process is interactive.

Sign up to request clarification or add additional context in comments.

8 Comments

When I start the seconds Matlab instance with: MatlabProcess = java.lang.Runtime.getRuntime().exec('matlab -nosplash -nodesktop'); then the command window is not started and I can read output from the spawned instance of Matlab through getInputStream(). I still cant write to the output stream with the above code though. The code seems to execute OK, but nothing appears to be written to the 2nd instance of Matlab.
@Greg: You don't write to the output stream, you write to the input stream (which means child process's stdin). Wait, no, the Java documentation is all screwed up.
Thanks for the response. I'm fairly new to Java and am confused by the idea of writing to an InputStream. As far as I understand, Java InputStreams don't have a write() method. Java OutputStreams do however: docs.oracle.com/javase/1.4.2/docs/api/java/io/OutputStream.html I must be missing something but am having a difficult time understanding what it is.
@Greg: Usually when you start a child process, you get a handle to its stdin, which you write to and the child process reads. And vice versa for the child processes stdout. And the docs use that terminology here: "failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block". But later "Gets the input stream of the subprocess. The stream obtains data piped from the standard output stream of the process represented by this Process object". It's a mess.
The "right" thing would be to have a method getStdin() which returns an OutputStream, and getStdout() would return an InputStream, and there would be no confusion.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.