Call matlab from Java in detail

  • 2020-04-01 02:38:23
  • OfStack

Some time ago groped for Java call matlab things, do not say how deep to learn, also calculate the results, to achieve the purpose. Also with the Java program can call matlab function.

      In that order, the first one is definitely going to be matlab. Which one? Starting with the next 7.0 feels new. Later just think not, now have 7.8. At the same time, there is also another version of the Internet information, that is, 2006 version, 2006a version, 2008b version, later from the Internet to find information, the original, matlab is a year two versions, called a version and b version. For example, MATLAB 7.2 (Release 2006a), I placed m7.8 or version 2009a. There is corresponding relation on the net, have interest can look for.

      First of all, I use Java to call matlab with matlab is provided by the MABLAB BUILDER JA tool, but also to tell you that this tool was not available before version 2006b, I think before Java may be through JNI through c to call matlab. So pay attention to the version when you download it. One more thing to note is that if you're using an AMD processor, not an Intel processor, you might not be able to run it after installing matlab. Our machine is with AMD processors, the solution is also very simple, is to give your machine the new build a system variable, the variable name is BLAS_VERSION, a variable's 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 know to see matlab help, just from the Internet to see what others write, about such an article is basically from a mother, I do not understand the place, all the same there is no. Later saw the matlab help document, really excited a, that is the most authentic. I'm going to try to follow its instructions step by step.

      First, let's introduce MATLAB®. Builder & # 8482; JA, it's MATLAB® The Compiler & # 8482; The extension product, with which you can wrap your m-code file, and package into a number of class files, so that your Java program can use matlab, in fact, m-codeiv is of course you want to call the matlab program.

    To be clear, if you are only on the machine where matlab is installed, run with Java calls by MATLAB® Builder & # 8482; Jar generated by JA, of course.

    What if you want to call these jars in Java on a machine without matlab installed? Is also possible, though, but you need to install matlab in the machine provided by the matlab Compiler Runtime (MCR), its installation file in C: / Program Files / / R2009a matlab toolbox/Compiler/deploy/under win32 (your installation directory) filename MCRInstaller. Exe.

    Preparation:

    Java environment, JAVA_HOME is set correctly. There is one other thing that might be wrong with 1.5 if you have a JDK version 1.6 or higher.

    Here's the key part, where you can see an example of an m-code function being transformed into a Java callable component.

    The name of this example is magicsquare, and this example gives you an idea of how to create a Java component called magicsquare, which basically consists of a magic class and a jar file, and other files that are used by the previous distribution components. Magic. Class is the most critical, which is that it is wrapped. The function makesqr of MATLAB is used to calculate the rubik's cube array.

      MATLAB Builder JA, you can call up in the MATLAB command, deploytool, in between, in order to verify that MATLAB has access to your Java environment, you can use getenv JAVA_HOME to verify that it is correct to return to your JAVA_HOME.

      This example, m code and Java code, matlab for us to write. All you need to do now is copy the code for it in your workspace, which is the example in

      Matlabroot/toolbox/javabuilder/Examples/MagicSquareExample.

    Matlabroot is the installation directory for matlab.

      Copy the MagicSquareExample folder into your workspace, for example, D:/javabuilder_examples

      Remember not to have Spaces. Rename the MagicSquareExample subdirectory to magic_square, and the file structure should now be D:/javabuilder_examples/magic_square. Then move the matlab workspace to D:/javabuilder_ examples/magic_square. Finally, to use m-code function, we need to test it first, open the makesq.m file, and then enter makesqr(5) on the matlab command line to see if the result is

17 and 24   1   8 15
Deploytool calls up MATLAB Builder JA, which should look like this,

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201312/20131226161106590.gif" >

Then you select MATLAB BUILDER JA, create a new work, click the Browse button to put your work in
D:/javabuilder_examples/magic_square. Enter magicsquare as the project name. So is the default project name
D:/javabuilder_examples/magic_square directory package name.
The folder name for MATLAB Builder JA assignment classes is set according to the project name, so in this case, Magicsquare is the directory for calsses
So the name, we're going to change it manually to class, right click on it. (this is also the reason why I used to read other people's articles and could not find their so-called class folder, and no one said this problem.)
Once the work is done, you can set your project, the matlab guide says, let's select "Generate Verbose Output" so you can see the error message, which is actually selected by default.
Then, add the m file to the project, find the makesqr.m file in matalb, and drag it to the magic folder of the project with the mouse. Then save the project.
Click the button at the top of the panel and wait for a while. Matlab will create two folders SRC and distrib under your project directory. The SRC directory contains the generated Java files, magic.class. The distrib directory includes the Java archive file, magicsquare.jar.
23   5   July 14, 16
  4   June 13 20 to 22
10 December 19 21   3
11 18 to 25   2   9
Here's how to create the m-code function as a Java component. Using what I just said,

Next, it's time to verify that the Java components you generate with matlab builder ja are working.

In your first Java ide to create a Java project, then just generated magicsquare. Jar, also, matlabroot/toolbox/javabuilder/jar/javabuilder jar to join your Java project 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 the test code to know, very easy.

You can test it. It prompts you to enter a number. As input 5, press enter to get

Magic square of order 5

17 and 24   1   8 15
23   5   July 14, 16
  4   June 13 20 to 22
10 December 19 21   3
11 18 to 25   2   9
That's right.


As a final note, when I run this example on the company machine, I can get results, but I always report them before I get results

< img border = 0 SRC = "/ / files.jb51.net/file_images/article/201312/20131226161238108.gif" >

This is a mistake, but in my house, I don't have this error on my own machine, and just to be clear, I have an INTEL CPU on my own machine, and the company has an AMD CPU, and nothing else. I don't know why.


Related articles: