Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running MATLAB function from Java

Tags:

java

matlab

I have an .m file in MATLAB which I would like to call from Java an get the solution as a string or whatever in Java. This sounds really simple but for some reason I can't make it work.

I tried this:

matlab -nosplash -wait -nodesktop -r  myFunction

but I'm not sure how I parse the answer since MATLAB opens it's own command line (in Windows).

I use this, but it doesn't return anything.

Process p = Runtime.getRuntime().exec(commandToRun);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

also it seems that every time I call MATLAB it opens a separate window which is a problem because I'd like to run this many times.

like image 530
Tom Avatar asked Oct 22 '09 15:10

Tom


People also ask

How do you call a Java function in MATLAB?

In general, use MATLAB syntax to call methods on Java objects. Alternatively, use Java syntax. javaMethod( StaticMethodName , ClassName , x1,...,xN ) calls the static method in class ClassName . In general, use MATLAB syntax to call static methods on Java objects.

Has an API for Java and can be integrated with MATLAB?

The MATLAB® Engine API for Java® enables Java programs to interact with MATLAB synchronously or asynchronously, including: Start and terminate MATLAB. Connect to and disconnect from MATLAB sessions on the local machine.

Is Java required for MATLAB?

You must have MATLAB Compiler SDK installed. Your Java environment must be properly configured. For details, see Configure Your Environment for Generating Java Packages. Your end users must have MATLAB or MATLAB Runtime installed to run compiled MATLAB code.

Which has an API for Java and can be integrated with MATLAB via Livelink?

COMSOL® API, which has all the necessary functions and methods to implement models from scratch. For each operation performed in the COMSOL Desktop® there is a corresponding command that is entered at the MATLAB prompt. It is a simplified syntax based on Java® and does not require any Java knowledge.


1 Answers

The trick is to use the MatlabControl class http://www.cs.virginia.edu/~whitehouse/matlab/JavaMatlab.html. It's very easy to use and you can do exactly what you're trying to do (and more).

like image 115
Jeff Storey Avatar answered Oct 21 '22 21:10

Jeff Storey