Java calls MATLAB

Source: Internet
Author: User

Thanks for sharing: http://blog.csdn.net/wannshan/article/details/5907877

Some time ago, I found out how to call MATLAB in Java. I did not know how deep I learned it, but I had some results to achieve my goal. That is, Java programs can call functions in MATLAB.

Let's talk about it in order. It must be the next MATLAB at first. Which one is next. At first, the next 7.0 thought it was quite new. Later, I thought no. Now there are 7.8. At the same time, another version number is also circulating on the internet, namely version 2006, version 2006a, and version 2008b. Later, I found out from the Internet that MATLAB was a two-year version, it is called version A and Version B. For example, in MATLAB 7.2 (release 2006a), I use M7.8 or 2009a. There is a corresponding relationship on the Internet. If you are interested, please refer to it.

First of all, I used Java to call the MATLAB tool provided by MATLAB. The mablab builder ja tool also told you that this tool was not available before 2006b, I think in the past, Java may call MATLAB through JNI through C. So pay attention to the version when downloading it. Another note is that if you are using an AMD processor instead of an Intel processor, you may not be able to run it after installing MATLAB. The specific performance is that it is disabled once it is turned on. Our company's machine is AMD's processor. The solution is actually very simple. It is to create a new system variable for your machine. The variable name is blas_version, and the variable value is D: // MATLAB 7 // bin // Win32 // atlas_athlon.dll (your MATLAB installation address ).

Then, we learned how to use mablab builder Ja. At first, we didn't know how to read the help of MATLAB. We only read what others wrote on the Internet. Such an article is basically from a girl, I don't understand anything. After reading the help documentation of Matlab, I was really excited. That is the most authentic. Next I will try to explain it step by step.

Let's first introduce MATLAB builder Ja. It is an extension product of MATLAB compiler. You can use it to package your m-code file, and package it into multiple class files so that your Java program can use MATLAB. In fact, M-codeiv is of course the MATLAB program you want to call.

It should be noted that, if you only run the jar package generated by MATLAB builder ja by calling Java on the machine where Matlab is installed, of course, you can.

What if you want to call these jar packages in Java on a machine without Matlab installed? Yes, but you must first install the MATLAB compiler Runtime (MCR) provided by MATLAB on the machine. Its Installation File is in C: /program files/Matlab/r2009a/toolbox/Compiler/deploy/win32(your installation directory named mcrinstaller.exe.

Preparations:

Java environment, java_home is set correctly. Another point is that if your JDK version is later than 1.6, 1.5 may be incorrect.

The key part is. In this part, you can use an example to see that an M-code function is transformed into a Java callable component.

The name of this example is magic square. This example shows you how to createMagicsquareJava component, which mainly includesMagicClass, and a jar file, as well as other files used by the release components.Magic. Class is the most critical, that is, it is packaged, MATLAB FunctionsMakesqr, The makesqr function is used to calculate the cube array. the Java program is used to call this function.

MATLAB builder Ja, which can be obtained through MATLAB commands,DeploytoolTo verify that MATLAB has obtained your Java environment, you can use getenv java_home to verify that it is correct to return your java_home.

In this example, M code, Java code, and Matlab are all written for us. What you need to do now is to copy its code to your workspace. It is an example in

MATLAB Root/Toolbox/javabuilder/examples/magicsquareexample.

MATLAB root is the installation directory of Matlab.

CopyMagicsquareexample folder to your workspace,For example,D:/javabuilder_examples

Do not have spaces. SetChanged the name of the magicsquareexample subdirectory Magic_square. The current file structure should beD:/javabuilder_examples/magic_square. That's right. Then convert the MATLAB workspaceD:/javabuilder _ examples/magic_square.Finally, to use the M-code function, we need to test it first and openMakesqr. M file, and then in the MATLAB command line, enter Makesqr (5) to see if the result is correct.

17 24 1 8 15
DeploytoolCall up MATLAB builder Ja, which should look like this,

 
Then you select MATLAB builder Ja, create a job, and click the Browse button to put your work in
D:/javabuilder_examples/magic_square. InputMagicsquareAs the project name. The default project name is also
D:/javabuilder_examples/magic_squareThe package name in the directory.
MATLAB builder ja assigns classes to store the folder name according to the project name, so in this work,MagicsquareIs used as the directory for storing calsses
Name. We need to manually change the name to "class". Right-click it and you can change it. (This is why I used to read other people's articles. I still cannot find the so-called class folder. No one said this problem)
After the work is established, You Can setting your project. In the MATLAB guide, let'sGenerate verbose outputYou can see the error information. In fact, it is selected by default.
Then, add the M file to the project and find it in matalb.Makesqr. mFile, drag it with the mouseEngineering magicFolder. Save the project.
The following is the building project. Click the top of the Panel

Button, wait for a while, Matlab will be under your project directory, createSRC And distrib.The src directory contains the generated Java file magic. Class. The distrib directory contains the Java archive file magicsquare. jar.

23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
The M-code function is created as a Java component. As mentioned earlier,

Next, verify that the Java component generated by using Matlab builder ja is usable.

First create a Java project in your Java IDE, and then put the generated magicsquare. jar, and,MATLAB Root/Toolbox/javabuilder/JAR/javabuilder. jar is added to 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 method is called. You can see the test code, which is easy.

You can test it. It prompts you to enter a number. If you enter 5, press enter to 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

It is correct.

 

 

Finally, when I run this example on a company machine, the results are displayed, but before the results are displayed, the results are always reported.


This error is not found on my own machine. It should be noted that Intel CPU is used on my own machine and AMD is used in the company, there is no other difference. I don't know why.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.