Executing a Matlab function using java - 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.

Related

How to use my created Matlab functions in java [duplicate]

I have an algorithm and some other code which is in MATLAB and I want to use it in my Android application.
How can I do this?
Can I make a jar file from MATLAB for use with Android?
I have to do something else?
If you have an additional product, MATLAB Builder JA for Java, you can produce a .jar file from your MATLAB code.
However, the .jar file requires the MATLAB Compiler Runtime (a freely redistributable component that you get with MATLAB Compiler and MATLAB Builder products) to be present. The MCR has a much larger footprint than is suitable for the typical Android device (it's like a copy of MATLAB itself, without the user interface).
You could think about either
Running your MATLAB .jar file remotely on a server, and having your Android application connect to it, or
Instead of using MATLAB Compiler and Builder products, use MATLAB Coder, which will convert a subset of the MATLAB language directly into C code. This C code doesn't require the MCR, and could be compiled to run directly on Android. Make sure your MATLAB algorithm falls within, or can be expressed in, the appropriate subset of the MATLAB language.
Edit: As of R2015a, functionality from MATLAB Builder JA for Java has been replaced by a new product, MATLAB Compiler SDK.
I am the developer of Addi. http://addi.googlecode.com Addi is quickly becoming a full port of Octave (which is an open source tool that uses Matlab syntax). Addi is going to have intents for other applications to use it as their math engines or plotting engines. So, if you can run your code on Octave, then very soon you will be able to run it on Android.
Our only option is to get C++ code from M code using MATLAB Coder toolbox, that generates standalone C and C++ code from MATLABĀ® code. It supports only some subset of all Matlab functions, therefore might be not suitable for your needs.
Having C code you can compile it using NDK. MATLAB Compiler is not an option here.
A new feature in Matlab 2014a:
http://www.mathworks.com/help/simulink/samsung-galaxy-android-devices.html
You can now directly install (limited set of) models to Samsung Android devices, and this should work actually on any Android device.
You can convert matlab code into python and then use the python code in the android .There are many tools to do this conversion. Python goes well with android than matlab.
You have 2 options,
Create a JAR and include in your Java Application and start using it. (I have not tested this by creating a JAR outside Eclipse)
You can code the same thing in C and use Android NDK to process it. (This will be faster and safer way)

How to use Matlab from JAVA without using the Matlab Compiler

I have used the JavaBuilder that comes with Matlab so that I can use Matlab functions from my JAVA program. In order to do that, I need to have the Matlab Compiler installed. Is there a way to do use Matlab from JAVA, but without the need to have the Matlab Compiler? I mean, can Matlab create the JAVA version of the matlab function (translate from matlab to JAVA)?
Thank you
If you have Matlab (but you don't have builderJA) then you can use matlabcontrol for using matlab functions in java.(There might be other alternatives too, but I don't know)
If you don't have Matlab at all then you can "create the java version of the matlab function" in some other computer having Matlab and builderJA installed, and use the jar files in your own computer. You only need to have MCR (Matlab Compiler Runtime- Its FREE) installed on your computer.
Strickly answering your question:
Is there a way to do use Matlab from JAVA, but without the need to
have the Matlab Compiler?
The answer is:
Yes, you can use it. Using matlabcontrol (A java API to interact with Matlab).
matlabcontrol is a Java API which will allow you to call MATLAB from Java. It does not make use of any compiler.
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.

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.

Calling Matlab from Java for heavy algorithms?

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.

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++.

Categories