Connect Java with Matlab - java

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.

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.

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.

Is it possible to run C source code from Java?

Now I have some C source codes, I would like to use it in my java application.
I need to execute the C source code, and get back the result to my java application. Instead of re-write all the C source code to java, how can I reuse the C's source code in my java application?
Take a look at Java Native Interface.
The Java Native Interface (JNI) is a programming framework that
enables Java code running in a Java Virtual Machine (JVM) to call and
to be called by native applications (programs specific to a
hardware and operating system platform) and libraries written in other
languages such as C, C++ and assembly.
There are the following ways.
JNI (see answer of #AurelioDeRosa +1)
JNA
If your C program can run as command line utility why not just to execute it using Runtime.getRuntime().exec() or ProcessBuilder?
Two options:
Make a library accessible via JNI or JNA
Make an executable, and call it with ProcessBuilder or Runtime.exec and capture the output streem if needed
Yes its possible Take a look at
Calling C Code from Java

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.

Categories