private static void invokeMatlab ( File dataMatrixFile , File affMatrixFile , int dimensions , File outputFile ) throws IOException { String commandLine = "matlab -nodisplay -nosplash -nojvm" ; LOGGER . fine ( commandLine ) ; Process matlab = Runtime . getRuntime ( ) . exec ( commandLine ) ; // Create the Matlab-specified output code for the saving the matrix String outputStr = "save " + outputFile . getAbsolutePath ( ) + " projection -ASCII\n" ; // Fill in the Matlab-specific I/O String matlabProgram = String . format ( SR_LPP_M , dataMatrixFile . getAbsolutePath ( ) , affMatrixFile . getAbsolutePath ( ) , dimensions , outputStr ) ; // Pipe the program to Matlab for execution PrintWriter stdin = new PrintWriter ( matlab . getOutputStream ( ) ) ; BufferedReader stdout = new BufferedReader ( new InputStreamReader ( matlab . getInputStream ( ) ) ) ; BufferedReader stderr = new BufferedReader ( new InputStreamReader ( matlab . getErrorStream ( ) ) ) ; stdin . println ( matlabProgram ) ; stdin . close ( ) ; // Capture the output. Matlab will not automatically finish executing // after the script ends, so look for the "Finished" text printed at the // end to know when to stop the process manually. StringBuilder output = new StringBuilder ( "Matlab LPP output:\n" ) ; for ( String line = null ; ( line = stdout . readLine ( ) ) != null ; ) { output . append ( line ) . append ( "\n" ) ; if ( line . equals ( "Finished" ) ) { matlab . destroy ( ) ; } } LOGGER . fine ( output . toString ( ) ) ; int exitStatus = - 1 ; try { exitStatus = matlab . waitFor ( ) ; } catch ( InterruptedException ie ) { throw new Error ( ie ) ; } LOGGER . fine ( "Octave LPP exit status: " + exitStatus ) ; // If Matlab was not successful throw an error to indicate the output // file may be in an inconsistent state if ( exitStatus != 0 ) { StringBuilder sb = new StringBuilder ( ) ; for ( String line = null ; ( line = stderr . readLine ( ) ) != null ; ) { sb . append ( line ) . append ( "\n" ) ; } throw new IllegalStateException ( "Matlab LPP did not finish normally: " + sb ) ; } }
Invokes Matlab to run the LPP script