I can call a .m file in Matlab from Java and that works fine, but I want to retrieve the results from Matlab and display them in Java. How can I do this?
matlabcontrol is a Java API which will allow you to do just that. 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
I have some mex files that urgently need to be called via MATLAB, there is currently no way around. However, I really despise MATLAB's GUI (in)possibilities and would like to create some e.g. JavaFX Apps.
My question: how can a Java app's communicate with a running MATLAB instance?
I know that you can include Java objects into MATLAB, however I would prefere to have a standalone Java app.
Java can execute commands via command line for example:
Process process = Runtime.getRuntime().exec(command);
process.waitFor();
So it is possible to execute a MATLAB script via command line in Java.
In MATLAB it is possible to write files with any data needed. I don't remember the exact way you may do this. http://www.mathworks.com/help/matlab/ref/fprintf.html gives an example:
x = 0:.1:1;
A = [x; exp(x)];
fileID = fopen('exp.txt','w');
fprintf(fileID,'%6s %12s\n','x','exp(x)');
fprintf(fileID,'%6.2f %12.8f\n',A);
fclose(fileID);
It is some kind of a workaround but it should work and it is not really hard to implement.
Update.
If Matlab is already running and you want to communicate with it in another application (Java), it may be done using a network connection through the localhost. Matlab may listen to some predefined port (for code example see http://www.mathworks.com/matlabcentral/fileexchange/11802-matlab-tcp-ip-code-example ) and do some action when a "start" trigger is sent via Java (or even some data along with the trigger). In Java you may use the Socket class (some code example may be found here http://www.javaworld.com/article/2077322/core-java/core-java-sockets-programming-in-java-a-tutorial.html ).
Also it may be done writing data into files. For example, Java adds some command to some file with predefined name (command.txt). Matlab scans this file in a loop and when something is found there it starts calculation (and Java application waits for results in some results.txt file).
I would suggest to start a server in Matlab that listens on a specific port to send/receive data to/from a Java client. By using the eval Matlab command you could even invoke scripts/command/etc. remotely controlled by a Java client.
You might want to have a look at this code example.
I need to access the windows native file upload and save window using java.
Though it is not possible only using java so I try this using jacob and JNA library.
Actually I also need to handle the any type of pop up message from OS.
There are lots of any other approach using java robot,sikuli,autoit but i want to stick(learn)
on jacob or jna.
Can any body help me regarding this problem.Any code snippet on this, helpful for me not only that if some body told me how to get hwnd ID dword ID which is need to pass into the different methods that also be useful for me.
Im guessing you were referring to CFileDialog or OpenFileDialog, well that can be accomplished via JNI and a native DLL which you need to compile yourself, which is somewhat easy:
create a C++ DLL - project in visual studio, i'd recommend using MFC - methods which are called via C-functions, that'll make everything MUCH easier since you wont need to re-invent the wheel and create WindowProcs yourself and whatnot - MFC does all of that transparently. You could also create a CLR-DLL which uses WinForms but thats a bit more complex. Do yourself a favor and disregard anything about MinGW, GCC or even any other OSS-solution if you want to create native windows ... that'll give you headaches, trust me. Within windows, you will need to use microsoft-libraries if you want to create Microsoft-compatible user interfaces, everything else is very error-prone.
include jni.h which is found in your JDK-library
write a JNI-compatible method signature
compile
declare compatible, native methods in java
compile
try to run your java project
report back, i shall help you to some extent if you run into any problems
JNI howto
I am a beginner in the field of Data Mining. I have a data which i need to process before visualizing any useful results. So I do the following:
Preprocess data using Java Program ( Output : txt files) (Input: FileNames)
Convert .txt files to .xls files ( Excel file)
Import excel file in MATLAB
Write Matlab Script to draw some plots on the imported data
I wish to automate this process by just pressing run button and then it happens automatically.
Please guide me how to integrate my work so instead of doing these 4 steps each time i just provide input file names and all the work is done in one go.
Thanks!
You can use arbitrary Java classes from within MATLAB - it is supported to run user-defined classes and even pass data between them and MATLAB. This would give you the following benefits:
no need to write text files/XLS and then import them into matlab
write everything in one MATLAB script - that is what duffymo had in mind, just that you do not need any shell/batch processing. Everything is done in MATLAB
you could add a MATLAB-implemented GUI to handle the process
Have a look at this SO post for some insights.
You want a scripting language: Windows command if you're on that platform; shell scripts on Linux.
It might not be a button, because that implies a UI. You have to write what amounts to an application for that.
The java matlabcontrol library might come in handy
http://code.google.com/p/matlabcontrol/
I had a similar task some time ago. Matlabcontrol is a library which connects MATLAB to your Java program and is quite easy to use.
All steps can be done in Matlab environment.
Java code can be called in Matlab directly. Just add your java class path to Classpath.txt in Matlab and import in the script code.
Maybe CSV data file is better for your case.
I am considering using libsvm. Currently, I have transformed my feature vectors to SVM feature vectors format. I am using LibSVM inside a project I will need to somehow use it within my app. The problem is that at the moment all examples available use the command line to provide input to the LibSVM. I don want this.
Can you please give me an example of how I can use it within my code (no calls to the command prompt and no Weka).
Thanks
There are examples in the distribution source code https://github.com/arnaudsj/libsvm/tree/master/java
The applet doesn't use command-line input. Check libsvm-3.11.tar.gz\libsvm-3.11.tar\libsvm-3.11\java
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.
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).
matlabcontrol is based on the same underlying MATLAB library used by MatlabControl mentioned by Jeff, but is more up to date, reliable, and documented. To get started, take a look at the walkthrough.
In Matlab R2016b, MathWorks added MATLAB Engine API for Java which allows to execute MATLAB code from Java.
JAMAL is an open source, Java RMI-based (Java Remote Method Invocation API) library that suits your needs
There exists a good Java-COM-Bridge called JaCoB (http://sourceforge.net/projects/jacob-project/) which you can use to automatically start Matlab as a COM-Server in the background. You can then follow the instructions in the Matlab help to interact with the Matlab COM Interface.
Although this is a very generic interface, it provides enough flexibility to easily do a few calls to Matlab like in your case.
Simply download the JaCoB package and look in the docs folder for some documentation.
You also have to include the Jacob DLL in your path.