Calling Matlab from Java for heavy algorithms? - java

Did anyone made a coupling between Java and Matlab and can advise how this can be done best? Is there eg a framework that I should not miss to look at?
I want to write a java program that hands parameters to a matlab function and gets the result (and pictures created in matlab based on iterations) back.
Further these results shall be displayed on a website, so Java EE JSF will be my choice.
How can this coupling be done best?

You could try JAMAL (JAva MAtlab Linking): http://jamal.khadkevich.org/about.html

Yes, the Mathworks have made a coupling between Java and Matlab and can advise how this can be done best. You might start your reading at this page.

matlabcontrol is a Java API which will allow you to call MATLAB from Java. You can either call MATLAB from within MATLAB or from outside MATLAB in which case an instance of MATLAB is launched and connected to. It allows for invoking eval and feval in MATLAB and returning the results to MATLAB. The walkthrough explains with examples how to do this. The walkthrough uses built-in MATLAB functions and commands, but you can use it with your own .m files because using matlabcontrol is just like interacting with MATLAB's Command Window.

Related

Connect Java with Matlab

I have created a Java gui and want to execute MATLAB (.m) functions to execute on click events of jButtons of my Java GUI. I searched on internet and found a way to do this by MATLAB Builder.
Is there any other way than MATLAB Builder which can be used to perform the same?
Yes, you can use JMI or JMatLink to interface with MATLAB from Java. Or, if you're more adventurous, you can roll your own JNI or JNA library to interface directly with MATLAB's C API.
matlabcontrol is a Java API which will allow you to call MATLAB from Java. You can either call MATLAB from within MATLAB or from outside MATLAB in which case an instance of MATLAB is launched and connected to. It allows for invoking eval and feval in MATLAB and returning the results to MATLAB. The walkthrough explains with examples how to do this.

Run m-files Inside Java

I have a matlab function written in an m-file. (it's not written by me and seems a complex algorithm) Now I want to use it with a java application. I searched in internet and learned how to run matlab codes inside java.
For example here it says how to do that. But I can't understand how to use the exact matlab function as it is in java without knowing what function does. Can someone help me to figure this out?
I am not sure that MatlabControl is what you need. It merely runs Matlab as server and sends it commands. You will not be able to give it to your users, unless they are all willing to pay for a Matlab installation.
If you want to deploy your application, consider using Matlab Builder JA.
From the website:
MATLAB Builder™ JA enables you to create Java™ classes from your MATLAB® programs. These Java classes can be integrated into Java programs and deployed royalty-free to desktop computers or Web servers that do not have MATLAB installed.
Also, if you use MatlabControl, there might be a compatibility problem in the future - check this out:
This API relies upon the Java MATLAB Interface distributed with all recent copies of MATLAB. This interface is entirely undocumented and there is no guarantee matlabcontrol will work with all versions of MATLAB and operating systems. Efforts are made to ensure compatibility with MATLAB R2007b and greater, see here for specifics.

Convert Java jar file in to cpp

I have a java code and created a jar file.
I need to create an Qt application. Can I use this code in that application?
Please help me how can i use that jar file.
Thanks,
Nagaraju.
You could take a look at the capabilities of GCC/GCJ (see http://gcc.gnu.org/ ). IF it's a good idea is a whole other story, and depends on what you have, and what you're trying to accomplish. It should be doable to link SO's created with GCJ in QT applications, but I seriously wonder if you are not better off using either C++ or Java, but not mixing them
If your Java code takes input from stdin or some file and writes output to stdout or some file, then the easiest way is to fork java to run that jar, and parse the output in your Qt code.
Things other than that, you'll need to be a bit specific. Something like "my Java code does painting the screen".
My advice is to use SWT or Swing.
You can use gcj gcj to compile the java code to library and simply call the functions of the java code from your C code.
Yes, you can use your jar file in your Qt application. I've done exactly this myself.
One way is to use the JNI Invocation API. This is part of the Java Native Interface (JNI), which makes it feasible but not pleasant to access Java APIs from C++.
A much more pleasant approach is to use CodeMesh JunC++ion, which wraps the Java APIs in C++ classes. This is a great product, if you can afford it.
If you have very little Java code, it may be easier to port it to C++.

Executing a Matlab function using java

I'm writing an application which does image processing using matlab and later displays the result using Java's Interface. Due to certain reasons I've to use both Java and Matlab.
How can I use the matlab function in java ?? How to create and access the interface.
matlabcontrol is a Java API which will allow you to call your image processing functions or scripts and return the results to Java. To get started, take a look at the walkthrough.
MATLAB Builder JA is one option to use MATLAB code in Java. This is a non free toolbox for creating a jar file from MATLAB code in order to be imported to Java. Take care of the restrictions concerning jar file creation.
Using Java classes inside MATLAB is much easier, as you can instantiate Java classes in MATLAB code. Undocumented Matlab is one valuable resource for Java integration in MATLAB.
Check Using Sun Java Classes in MATLAB Software for the official information provided by The MathWorks.
A quick google brought up this http://j-integra.intrinsyc.com/support/com/doc/other_examples/Matlab.htm
Alternatively, can you execute your matlab function from the command line? If so you can use
Runtime.getRuntime().exec("your matlab function")
As of R2016b you can use official MATLAB Engine API for Java, which seems to allow same functionality as matlabcontrol.

Java Wrapper to Perl/Python code

I have to deploy some Web Services on a server that only supports the Java ones, but some of them will be done using perl or python. I want to know if is possible to develop a Java wrapper to call a specific code written in perl or python. So, I want to have all the Web Services in Java, but some of them will call some code using other languages.
Thanks in advance.
Regards,
Ukrania
This depends heavily upon your needs. If Jython is an option for the Python code (it isn't always 100% compatible), then it is probably the best option there. Otherwise, you will need to use Java's Process Builder to call the interpretters directly and return the results on their output stream. This will not be fast (but then again, Jython isn't that fast either, relative to regular Java code), but it is an extremely flexible solution.
For the Python part of it you can use Jython to run Python code right from your Java virtual machine. It'll integrate fully with your Java code as a bonus.
For Perl, use Inline::Java. There are several options for integrating the code; you can call a separate process or you can use an embedded interpreter.
For Python you can use the Java Scripting API.
A Perl implementation is sadly still missing.
There's something I used a while back called Jython which allows you to execute Python code from Java. It was a little quirky, but I got it to do what I needed.
http://www.jython.org

Categories