to connect matlab with java
- by user304005
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();
}
}