Hybrid programming applications of Java, VC ++, and Matlab

Source: Internet
Author: User
Chen Yongzhou, Department of Mechanical Engineering, Southeast University  
  With the rapid development of Internet/Intranet Communication Technology, the development and use of distributed and heterogeneous integrated application systems are inevitable. Java has the advantages of being stable, reliable, and cross-platform, using it to develop distributed computing systems is an inevitable trend. At the same time,

 

Java Development must inherit and integrate existing systems. Therefore, communication between Java and existing software systems is now on the agenda.

 

 

 

 

 

MATLAB is an interactive software developed by Mathworks for numerical computing. It has powerful numerical analysis, matrix calculation, signal processing, and graphic display functions; with its powerful data processing capabilities and rich toolbox, Matlab makes his programming extremely simple, which can greatly shorten the application development cycle and improve programming efficiency. Therefore, using VC ++ to implement MATLAB graphics processing and display is a feasible method to improve efficiency. The communication between Java and Matlab must be solved in two aspects: Java and VC ++; and VC ++ and Matlab.

 

 

The functions of this system (a proxy server in a distributed network multi-proxy system-an artificial neural network proxy server) are as follows: java programs in the JBuilder Development Environment call the DLL containing the MATLAB graphics processing and display function to display images. Among them, vc needs the "title. M, xlabel. M, ylabel. m" files of MATLAB and the "errorpicture. m" files for graphic processing and display ". The development environment of the system is Windows XP, VisualC ++ 6.0, jbuilder6.0 development tools, and Matlab.

 

 

1. Introduction to Java-VC communication methods

 

 

The interaction between Java and VC can be communicated through the files in the agreed format written and written by the programs separately run, but the real-time communication cannot be guaranteed, communication by defining the client/server of the IDL Interface increases the complexity of implementing simple functions of the system. You can also use JNI (Java Native Interface) or RMI (Remote Method invoCATion.

 

 

InMicrosoftThe JNI mechanism is convenient, fast, stable, and reliable for communication between local programs, and can realize real-time communication. This mechanism is used for the implementation of the system in this example.

 

 

1.1 interconnect Java with VC ++ (DLL)

 

 

First, create a project in JBuilder, such as mass. Use massframe as the class name of the frame.

 

 

To use the DLL for Matlab graphics processing implemented in VC, add the following code to the class massframe of the Java file:

 

 

Public native void showpicture ();

 

 

// Implement the function declaration in the called DLL

 

 

Static

 

 

{

 

 

System. loadlibrary ("showpicture ");

 

 

// Use the DLL file named "showpicture. dll"

 

 

}

 

 

Void jbshowpicture_actionreceivmed (actionevent E)

 

 

{

 

 

Showpicture ();

 

 

// Use of functions in DLL in Java program

 

 

}

 

 

1.2 System Operation settings and compilation

 

 

You must set the compilation attribute of the Java file using the Matlab function in the DLL, and find the class massframe in the project view area of the JBuilder tool. java (the file names determined by developers may be different). Right-click the file and select "properties" from the menu that appears, click "Build-> RMI/JNI-> Generate JNI header file", find the "generate JNI header file" project, and select. Compile the file to obtain the corresponding H file. The system generates "mass _ massframe. H", in which the called function is: "jniexport void jnicall java_mass_massframe_showpicture (jnienv *, jobject );".

 

 

If you only useSunTo implement JDK in the company, you must use javah.exeto generate the corresponding H file for the class file compiled by javac.exe, and then modify the H file to display the package level of the file in the called function name, that is, "jniexport void jnicall java_massframe_showpicture (jnienv *, jobject);" is changed to the name of the called function above.

 

 

1.3 Java application system running

 

 

Copy the DLL file generated by VC to the directory of the current running program of JBuilder, and execute the program to implement the communication between the Java System and Matlab. In this example, the system DLL is "showpicture. dll" (that is, the following "Introduction to implementation of VC and Matlab communication methods ). Figure 1 shows the running result:

 

 

 

 

 

 

 

 

 

Figure 1 Image Display of Java calling DLL

 

 

 

2 Introduction to VC and Matlab communication methods

 

 

MATLAB provides many methods for communication with VC programs: the Matlab Engine mode adopts the client/server computing mode and uses the Matlab application interface (API) the Matlab function is called in the VC environment. However, this method requires the background support of the MATLAB environment. Therefore, the system must install MATLAB. The MCC tool provided by MATLAB is compiled, compile M file to generate VC ++ or C language, and use it directly in VC. the Mex tool of MATLAB compiles M file into DLL for VC to call ......

 

 

MATLAB's existing MATLAB add-in (which encapsulates MCC and Mex) makes the application of the second and third methods more convenient. Now the implementation of this system is based on the third method.

 

 

2.1 implement interconnection between MATLAB and VC

 

 

To call the specific functions of MATLAB during development, you should first install the MATLAB tool software in the operating system, then run the tool software, and then execute "Mex-setup; mbuild-setup; CD(PRefdir); mccsavepath; "command. Run the VC development tool and choose tools> customize> add-ins and macro files from the VC ++ menu. Select the MATLAB add-in Macro, you can see one more in the toolbar.

. Now VC can use it to obtain the C and C ++ files corresponding to M files.

 

 

2.2 Development Tool VC running environment settings

 

 

During development, you must set the development environment variables of VC. Add environment variables to the "directories" item found in "tools-> options-> directories.

 

 

Add the header file and the library file search path (the file search path of this computer is ):

 

 

F:/maid/extern/include

 

 

F:/maid/extern/include/CPP

 

 

At the same time, add the Link Library of MATLAB to VC. Select "library files" from the "tools-> options-> directories" "show directories for:" drop-down box, and then add the environment variables in the "directories" item.

 

 

F:/maid/extern/lib

 

 

F:/maid/extern/lib/Win32

 

 

F:/maid/extern/lib/Win32/Microsoft/msvc60

 

 

The above is the environment where C code is generated for the M file compiled by VC in MATLAB. To compile the hfile of JNI, you must add the following header file and library file search path through the menu tools. And project:

 

 

D:/jbuilder6/jdk1.3.1/include

 

 

D:/jbuilder6/jdk1.3.1/include/Win32

 

 

2.4 DLL generated by VC

 

 

The system first uses VC to create a project named showpicture in "Matlab Project Wizard". The project option result is shown in 2:

 

 

 

 

 

 

 

 

 

Figure 2 Project Creation option results

 

 

 

Copy the mass _ massframe. h file generated in 1.2 to the system directory and add it to "headfiles" in "FileView" of VC.

 

 

To provide callable functions for Java programs, add the following in the "showpicture. c" file:

 

 

# Include "mass _ massframe. H"

 

 

// The Implementation of the Java call function exported by DLL

 

 

Jniexport void jnicall

 

 

Java_mass_massframe_showpicture

 

 

(Jnienv * en, jobject ob)

 

 

{

 

 

Showpictureinitialize (); // Initialization

 

 

Mlferrorpicture (); // Image Display

 

 

Mlfhgwaitforfigurestodie (); // disable displaying related resources

 

 

}

 

 

"F7": Compile the program and get "showpicture. dll ".

 

 

2.3 remove the support files from the MATLAB environment

 

 

To use a DLL that can use its functions without installing the Matlab application software, you must add some files to the directory that runs the DLL, these files can be obtained through "Matlab add-in packager" in the toolbar. Run according to the default configuration of this tool to obtain a zip compressed file. When used, decompress it to the directory where the DLL file is located, and the DLL can be used out of the MATLAB environment.

 

 

3 conclusion

 

 

When using the java system to provide external services, Matlab is used to complete the software core algorithm design, graphic processing, and display. This greatly reduces programming difficulty and workload, and shortens the development cycle, it also improves the software reliability. Communication between Java and MATLAB based on VC ++ 6.0 -- using the VC compiler to call a dynamic connection library containing the MATLAB processing function to implement close integration between Java and Matlab running in the MATLAB environment, it greatly expands the application scope of MATLAB and also deepens the application capabilities of Java and Matlab.

 

 

 

 

 

Note: The content of the errorpicture. M file is as follows:

 

 

Function errorpicture ()

 

 

S = 'nn/training error result .txt ';

 

 

FID = fopen (S, 'R ');

 

 

While (feof (FID ))~ = True

 

 

[X, N] = fscanf (FID, '% 12f ');

 

 

End;

 

 

If (n> 1)

 

 

T = * 100: N * 100;

 

 

Plot (t, x );

 

 

Ylabel ('absolute value of error ');

 

 

Xlabel ('training times (times )');

 

 

Title ('artificial neural network training error result display fig ');

 

 

Grid;

 

 

End;

 

 

Fclose (FID );

 

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.