How to run MATLAB code from Java?
I am facing problem in running MATLAB co开发者_开发问答de from Java. If you know something on this, could u please help me out?
If you don't want to buy MathWork's MATLAB Builder for Java Toolbox, you can use MATLAB's undocumented JMI (Java-MATLAB Interface). Search the CSSM newsgroup (or Google) for JMI, or JMatLink which is based on JMI. The MATLAB code itself is full of JMI usage examples - search "jmi" using the MATLAB Editor's search tool over the entire MATLAB installation.
Note that JMI is neither documented nor officially supported by MathWorks. Moreover, if improperly used it can hang or crash your application. But it's free, unlike the toolbox.
Instead of using the undocumented JMI (Java MATLAB Interface) mentioned by Yair, you can use matlabcontrol which is a Java API that wraps around JMI which makes it much easier to use. It is well documented and has an introductory walkthrough. Unlike directly using JMI, it can also be used when running a Java program outside MATLAB.
I have used Java Builder for running matlab scripts/functions in java application, and it seems to be very easy and helpful.
For this purpose, you need to have Java Builder JA toolbox to deploye matlab script/function as java class.
You then need to add the (converted) java class in to your java project library, and additionally the javabuilder jar file.
After doing the above steps you can easily run the matlab script (which is now a java class in the project library) just by calling the class name in the code. If the class is a matlab function, you can pass the paramenters and get the return values easily.
But offcourse, it is not the only method to do it. But this one seems easy to me comparatively.
I hope this answers to your question.
Here is a package that allows you to deploy MATLAB code as java classes.
There are 2 ways for doing this :
- Use the Matlab Engine which has a C interface (see the link of the first answer for that). It is mandatory to have Matlab installed on all the machines on which you want to run this (see Matlab license for details)
- Use the Matlab Builder for Java which compiles the matlab code and produce a Java interface of your functions directly. With this solution, you need to by the Matlab compiler and the Matlab Builder for Java but you can then deploy your solution without paying anything more. Note that you must install the Matlab Compiler Runtime (MCR) on each machine but this is free.
精彩评论