Call the Matlab function in a Java program

Source: Internet
Author: User
Posted on kid. Loki read (2814) Comments (3) EDIT favorites

Environment preparation

1. There are eclipse 3.1 and Matlab 2007a on the machine.

2. machine environment variable settings
(1) java_home (JDK installation location, such as C:/program files/Java/jdk1.5.0)

  • After setting, restart MATLAB to make it effective.
  • Use getenv java_home to test in the MATLAB command window. If the returned value is correct, it indicates that it has taken effect for Matlab.

(2) classpath

  • Add Maid/toolbox/javabuilder/JAR/javabuilder. Jar

(3) path

  • Add % java_home %/bin/javac

Generate the M file of Matlab into jar

1. Enter deploytool in the Command window of MATLAB. A new window (Deployment Tool) is displayed on the right ).

2. In deployment tool, click the new button and select MATLAB builder for Java and Java package. Create a project name, such as flying. prj.

3. Ensure that the generate verbose output in the deployment tool panel is hooked.

4. The M file to be called by Java (such as mydraw. m, which includes two parameters (x, y) from the left-side working directory Panel of the MATLAB interface, drag to the class folder under the new class in deployment tool.

5. Click the build button to generate a folder with the same name as the project under the current MATLAB directory. If the build is successful, the folder structure is shown as follows:
Flying ----- | ---- distrib
| ----- Flying. Jar
| ----- Flying. CTF
|
| ------- SRC
| ---- Flying
| ---- Flyingclass. Java
| ---- Flyingmcr. Java
| ---- Classes
| ---- Flying
| ---- Flyingclass. Class
| ---- Flyingmcr. Class
| ------- Build. Log
| ------- Flying. CTF
| ------- Flying. Jar
| ------- Mccexcludedfiles. Log
| -------Readme.txt

Compile the Java method and call the Matlab function in the generated flying. jar.

1. Add two jar packages to buildpath of the Java project attribute:

  • MATLAB Root/Toolbox/javabuilder/JAR/javabuilder. Jar
  • Testdirectory/Flying. Jar

2. Compile a function as follows:

Code
1 package test;
2
3 /***//**
4 * test for a MATLAB call
5 *
6 */
7
8 Import com. Mathworks. toolbox. javabuilder .*;
9 Import flying .*;
10
11 public class testmatlab class {
12
13 /***//**
14 * @ Param ARGs
15 */
16 public static void main (string [] ARGs ){
17 // todo auto-generated method stub
18
19 try {
20 system. Out. println ("begin ");
21 flyingclass flyingdraw = new flyingclass ();
22 system. Out. println ("Middle ");
23 flyingdraw. mydraw (7,2 );
24 system. Out. println ("here ");
25} catch (exception e) {system. Out. println (E );}
26}
27}

3. if it cannot run properly, you can add the flying. Jar location to classpath.

Details

1. See the Help File of Matlab.

2 www.simwe.com/forum/archiver/tid-747229.html

3. Data Types

Java numeric array, which can be passed directly as an input parameter to the. M file.

For example, mydraw (x, y) can draw a line chart like x = [1 2 3 4], y = [3.3-5 6 10.2. When Java calls this method, if the passed parameter is an integer or a real array, it can be directly successful. For example, in Java:

1int [] A = new int [4];
2int [] B = new int [4];
3 // assign values to A and B...
4 //...
5 // call
6 flyingclass flyingdraw = new flyingclass ();
7flyingdraw. mydraw (A, B );

Note: A and B are recommended to have values at all indexed locations. Otherwise, if there is not a sufficient value, the curve may eventually return to (0, 0.

MATLAB program (. M file) Modification

1 foundTestdirectoryThe location of/flying. Jar (because the. m source file is near this location according to the above steps ). This location can be seen from the package release e of Eclipse.

2. directly modify the. M file to be modified.

3. Open MATLAB and enter deploytool in command. In the newly opened deployment panel, "open" The project where the M file is located, such as flying. prj.

4. Select the. M file in the class folder of the corresponding project, and click build in the toolbar.

5. The corresponding source file is re-compiled. If you follow the jar package added in the previous step, the new jar package automatically replaces the original jar package after compilation. Because the location of the jar has already been used as buildpath to tell the corresponding project of Java, the Java end can correctly call the MATLAB method with the newly modified content without any adjustments.

Related Article

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.