to connect matlab with java

Posted by user304005 on Stack Overflow See other posts from Stack Overflow or by user304005
Published on 2010-03-29T07:00:47Z Indexed on 2010/03/29 7:03 UTC
Read the original article Hit count: 294

Filed under:

Through the below given code I was able to connect to matlab. But I was not able to execute the script file containing matlab code...Please help me to modify the code so as to execute the matlab code.... Here luck2 is a .m file....

import java.io.InputStreamReader; import java.io.*; public class matlab { private static File myMATLABScript; public static String runScript(File luck2) { String output = "" ; String error = ""; try {

        String commandToRun ="C:\\Program Files\\MATLAB\\R2009a\\bin\\matlab -nodisplay <" + "Z:\\sem\\java\\luck2";
        //String commandToRun = "matlab  -nosplash -r myMATLABScript -nodisplay -nodesktop < " + opentxt; 
        System.out.println(commandToRun); 
        Process p = Runtime.getRuntime().exec(commandToRun); 
        String s; 

        BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); 
        BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); 
        System.out.println("\nHere is the standard output of the command:\n"); 


        while ((s = stdInput.readLine()) != null) 
        { 
            System.out.println("haiiiiiiiiiiii");
            output = s + "\n"; 
            System.out.println(s); 

        } 

        while ((s = stdError.readLine()) != null) 
        { 
            error = s + "\n"; 
            System.out.println(s); 
        } 
    } 

    catch (Exception e) 
    { 
        System.out.println("exception happened here what I know:"); 
        e.printStackTrace(); 
        System.exit(-1); 
    } 
    return output + error; 
} 

public static void main(String[] args) throws IOException
{ 
    matlab m = new matlab(); 
   matlab.runScript(myMATLABScript); 
   //matlab.runScript(); 


} 

}

© Stack Overflow or respective owner

Related posts about java