+ Notes for calling MATLAB Functions in Java

Source: Internet
Author: User
1. the MATLAB version must support Java

Run deploytool in command mode. If this command is supported, use

 

2. Function writing in MATLAB

 

% Defines a function operation (a, B), calculates the addition, subtraction, multiplication, division, and result of A and B.

% Function definition function output variable list [s, M,...] function name (input variable list) sum, sub, Mul, div

Function [Sum, sub, Mul, div] = operation (A, B );

Sum = A + B;

Sub = A-B;

Mul = A * B;

DIV = A/B;

End

 

PS: The red part above is related to the return value of calling the operation in Java. The object result [] = xxx called in Java. operation (4, a, B); 4 indicates that four return values are stored in result [0] to result [4.

 

3. Use MATLAB to compile and generate jar

Enter deploytool In the MATLAB command window and a compilation window will pop up,

Click file and select new-deployment project,

Then select MATLAB builder Jave.

Enter the project name,

Ocation: Enter the storage location of the project.

Click OK.

Then, add the M file to the project. First, create a new class. The class name is the name of the new class you will need in Java, that is, the Class Name of operation in MATLAB, and then add your M file to this class directory, and then build it directly.

 

4. Use MATLAB Functions in Java

Now there is a new Java project under eclipse, and XXX. jar andUnder your MATLAB installation directory/Toolbox/javabuilder/JAR/javabuilder. jar is added to your Java project extend Lib.

 

Then you can clearly see the functions and classes contained in XXX. jar.

 

[Java]View plaincopyprint?
  1. Import com. Mathworks. toolbox. javabuilder. mwexception;
  2. Import com. Mathworks. toolbox. javabuilder. mwnumericarray;
  3. Import XXX. xxxx;
  4. Class test {
  5. Publicstaticvoid main (string [] ARGs ){
  6. Try {
  7. XXXX abc = new xxxx ();
  8. Object [] result = NULL;/* stores the result */
  9. Int A = 10, B = 2;
  10. Result = ABC. Operation (4, A, B );
  11. System. Out. println (result [0]. tostring ());
  12. System. Out. println (result [1]. tostring ());
  13. System. Out. println (result [2]. tostring ());
  14. System. Out. println ();
  15. } Catch (mwexception E1 ){
  16. // Todo auto-generated Catch Block
  17. E1.printstacktrace ();
  18. } Finally {
  19. /* Free Native Resources */
  20. System. GC ();
  21. }
  22. }

If result [0] returns an array, you can solve this problem using the following method:

 

Mwnumericarray A = new mwnumericarray (double. valueof (result [0]. tostring (), mwclassid. Double );

Mwnumericarray temp = (mwnumericarray) result [0];

Float [] [] weights = (float [] []) temp. tofloatarray ();

 

 

5. Possible Problems

Exception in thread "Main" Java. lang. unsatisfiedlinkerror: failed to find the library libmwmclmcrrt. so.7.13, required by MATLAB builder Ja, on java. library. path. this library is typically installed along with MATLAB or the MCR, its absence may indicate an issue with that installation or the current path configuration. the MCR version that this component is trying to use is: 7.13.

 

 

Failed to find the library mclmcrrt710.dll, required by MATLAB builder Ja, on java. Library. Path

 

 

The reason is that 2009a needs to install mclmcrrt7.10. Find \ toolbox \ compiler \ deploy \ win32in the installation directory of Matlab, find mcrinstaller.exe to install it, and restart eclipse.

 

You may also encounter:

Exception in thread "Main" Java. Lang. unsatisfiedlinkerror: Can't Load Library: D: \ Program Files \ MATLAB \ r2009a \ bin \ Win32 \ nativemcl. dll
At java. Lang. classloader. loadlibrary (unknown source)
At java. Lang. runtime. load0 (unknown source)
At java. Lang. system. Load (unknown source)
At com. Mathworks. toolbox. javabuilder. Internal. mwmcr. <clinit> (mwmcr. Java: 1449)
At operation. operationmcrfactory. <clinit> (operationmcrfactory. Java: 99)
At operation. Operation. <init> (operation. Java: 61)
At javatestmatlab. Main (javatestmatlab. Java: 18)

The nativemcl. dll file cannot be found. My system is a 64-bit system, so the win64 folder is under the bin directory. To adapt, I created a Win32 folder under the bin directory and copied nativemcl. DLL to it.

The following error is reported:

Exception in thread "Main" Java. lang. unsatisfiedlinkerror: D: \ Program Files \ MATLAB \ r2009a \ bin \ Win32 \ nativemcl. DLL: Can't load amd 64-bit. DLL on a Ia 32-bit Platform
At java. Lang. classloader $ nativelibrary. Load (native method)
At java. Lang. classloader. loadlibrary0 (unknown source)
At java. Lang. classloader. loadlibrary (unknown source)
At java. Lang. runtime. load0 (unknown source)
At java. Lang. system. Load (unknown source)
At com. Mathworks. toolbox. javabuilder. Internal. mwmcr. <clinit> (mwmcr. Java: 1449)
At operation. operationmcrfactory. <clinit> (operationmcrfactory. Java: 99)
At operation. Operation. <init> (operation. Java: 61)
At javatestmatlab. Main (javatestmatlab. Java: 18)

When using eclipse for development, one jar package must use JNI to reference the DLL file. This error occurs.

  1. Can't load amd 64-bit. dll on a Ia 32-bit Platform
My system is win7 64-bit, I found that eclipse uses the JRE is a 32-bit solution to run using jre_x64, put the DLL file into the project below: http://www.oracle.com/technetwork/java/javase/downloads/jre-6u26-download-400751.html Configuration:

 

Select jre_x64 runtime when creating a project:

End

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.