Some time ago to explore the Java invoke matlab things, do not say how deep the study, also counted the results, to achieve the purpose. The Java program is also used to invoke functions in MATLAB.
In order to say it, the beginning must be the next MATLAB to see. Which one is next. Starting with the next 7.0 feels very new. Later I thought it was not, and now there are 7.8. At the same time also spread on the Internet another version of the information, that is, 2006 version, 2006a version, 2008b version, and then find information on the Internet to know that the original, MATLAB is a year two edition, called A and B version. For example, Matlab 7.2 (release 2006a), I am under the m7.8 is the 2009a version. There is a corresponding relationship on the Internet, you are interested to find out.
First of all, I use the Java invoke MATLAB is provided by the MATLAB, Mablab BUILDER ja tool, but also to tell you that this tool 2006b version was not before, I think the previous Java may through the JNI through the C to invoke Matlab. So you should pay attention to the version when you download. There is also a point to note, if you are using AMD's processor, not Intel's processor, the installation of MATLAB, may not be able to run, the specific performance is, a turn off. My company machine is AMD's processor, the solution is actually very simple, is to give your machine to create a new system variable, variable name is blas_version, variable value is D://matlab7//bin//win32//atlas_ Athlon.dll (your MATLAB installation address).
Then, is that we learn to use Mablab BUILDER JA, began not to know to see the MATLAB help, just from the internet to see others write things, about such an article basically from a womb, I do not understand the place, are the same without. Later saw the MATLAB help document, really excited a, that is the most authentic. Next I try to follow its instructions step-by-step.
First, introduce Matlab®builder™ja bar, it is matlab®compiler™ extension products, with which you can wrap your M-code file, and packaged into a number of class files, so that your Java program can use MATLAB, Actually is such, m-codeiv of course is you want to call MATLAB program.
To be clear, if you are only installing the MATLAB machine, using Java invoke to run the jar package generated by Matlab®builder™ja, of course.
What if you want to invoke these jar packs in Java without installing the MATLAB machine? Yes, but you have to install MATLAB on the machine to provide the MATLAB Compiler Runtime (MCR), its installation files in C:/Program files/matlab/r2009a/toolbox/compiler/ Deploy/win32 (under your installation directory) filename MCRInstaller.exe.
Preparatory work:
JAVA environment, Java_home set correctly. A little more than 1.6 of your JDK version, 1.5 may be wrong.
The key section is here, and in this section you can, through an example, see a M-code function transform into a Java callable component.
The name of this example, Magic Square, lets you learn how to create a Java component called Magicsquare, which consists primarily of a magic class, a jar file, and other files used by the release components. Magic. Class is the most critical, that is, it is packaged, MATLAB function Makesqr, MAKESQR function is the calculation of Rubik's Cube array, the back of which we are Java programs, call this function.
Matlab Builder JA, can be passed in the MATLAB command, Deploytool, in between, in order to verify that MATLAB has been able to get your JAVA environment, you can use, getenv java_home to verify that you can return to your Java_ Home is the right thing to do.
This example, the M code and Java code, MATLAB to help us write well. All you need to do now is copy the code into your workspace, which is an example
Matlabroot/toolbox/javabuilder/examples/magicsquareexample.
Matlabroot is the installation directory of MATLAB.
Copy magicsquareexample This folder to your workspace, for example, D:/javabuilder_examples
Remember not to have spaces. Rename the Magicsquareexample subdirectory to Magic_square, and now the file structure should be d:/javabuilder_examples/magic_square. That's right. Then the MATLAB workspace transfer to D:/javabuilder_ Examples/magic_square. Finally, to use the M-code function we have to test it, open the Makesqr.m file, and then in the MATLAB command line, enter MAKESQR (5) to see if the result is
17 24 1 8 15
Deploytool pull up the MATLAB Builder JA, it should be as follows this appearance,
Then you choose Matlab BUILDER JA, create a new job, click the Browse button to put your work in
D:/javabuilder_examples/magic_square. Enter Magicsquare as the project name. The default project name is also
The name of the package under the D:/javabuilder_examples/magic_square directory.
MATLAB Builder JA Allocation Classes folder name is set according to the project name, so in this work, Magicsquare is as, calsses storage directory
Name, we have to manually change its name, class, directly right click can be changed. (This is what I used to read other people's articles, the old can not find their so-called class folder reasons, and no one said this problem)
After the work has been built, you can setting your project, the MATLAB guide says, let's Select the Generate Verbose Output option so that we can see the error message, in fact it is chosen by default.
Then, add the M file to the project, find the makesqr.m file in the Matalb and drag it to the Magic folder of the project with the mouse. Then save the project.
Below is the construction project, click on the top button of the panel, wait for a while, MATLAB will be your engineering directory, create SRC and distrib two folders. The SRC directory contains, generated Java files, Magic.class. The Distrib directory contains Java archive files, Magicsquare.jar.
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
The following is the creation of the M-code function as a Java component. With what I just said,
Next, is to verify that you use Matlab builder JA generated Java components is not the time to use.
First build a Java project in your Java IDE, and then take the Magicsquare.jar that you just generated, and matlabroot/toolbox/javabuilder/jar/ Javabuilder.jar Join your Java Engineering build path.
Then you copy the Getmagic.java test file under D:/javabuilder_examples/magic_square/magicdemojavaapp to your Java project and run it directly. The specific is how to call, you look at a test code to know, very easy.
You can test it. It is prompted to enter a number. If input 5, enter get
Magic Square of Order 5
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
is correct.
Last but not least, when I run this example on a company machine, I can produce results, but before the results are always reported
This is wrong, but in my home, there is no such error on their own machines, to illustrate the point is that my home on the computer of the Intel CPU, the company is using AMD, the other is no different. I don't know what the reason is.