Accessing Matlab from Java

IT Add comments

Today i had to access a matlab script from a java program. After some hours of trying to get it work by using matlabs command line i found a method to call a matlab script without using the command line. Using matlab -r “functionName” didn’t work because of some unknown charset erros i haven’t found till now. The basic idea is based on this blog-post.

Therefore, a process must be created:


String command = "matlab -nosplash -nodesktop";
Process process = Runtime.getRuntime().exec(command);

Afterwards we obtain the standard-input of the process and send a command to be interpreted by matlab:


BufferedWriter out = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
out.write("function_to_be_executed");
out.write("\n");
out.write("exit;") // this will close matlab after the execution finished!

If you like to see what is happening, try to read from the process.getInputStream()

hope that helps some of you!

Comments are closed.

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS