MATLAB is a numerical computing and graphics processing software launched by math works in 1984, it provides computing and graphic display functions in several fields, including numerical analysis, optimization, statistics, partial differential equation numerical solutions, automatic control, signal processing, and image processing. The integration of Control System Toolbox provides an effective way to design and analyze various complex control systems.
However, due to the limitations of the MATLAB development platform, the program developed above cannot be separated from the MATLAB runtime environment, so it is not flexible enough to deal with some practical application problems, to a certain extent, VC ++ can make up for this vulnerability. Therefore, the two can be used together and shared for a long time, it can provide more powerful technical support for scientific research and engineering development.
1. Implementation of MATLAB and VC mixed programmingMATLAB is a general tool designed for the control system. It can be easily connected with VC. Generally, there are three main methods to implement MATLAB and VC mixed programming: (1) using the engine and the computing mode of the client server, use the ActiveX channel of window to connect with Matlab. During specific applications, the program framework is often designed in VC, and compiled programs are used as front-end clients. by calling the Matlab Engine, the system can connect to the server in the background to achieve dynamic communication. (2) Using the MATLAB mathematical function library, Matlab contains a wealth of functional libraries, and provides mathematical function interfaces with VC, you can easily call it in the vc ide (integrated development environment. (3) Implement mixed programming of VC and MATLAB through DLL. DLL is an executable binary file. Put a lot of common functions in DLL, which can be called by multiple applications. This can reduce the amount of external storage space and achieve code sharing. Among the above three methods, the first method is very practical for calling the MATLAB toolbox. When designing and analyzing the control system, it is generally done by calling the Matlab Engine). This article mainly introduces how to use the engine to implement the mixed programming of VC and Matlab.
2. Introduction to the Matlab EngineThe Matlab Engine is a combination of a series of programs provided by MATLAB. It allows users to call MATLAB in their own applications and uses MATLAB as an operating engine to run it in the background. Generally, the Matlab Engine uses the Client/Server (Client/Sever) computing mode, which is a computing mode that distributes the application processing load to the client and server, the client and server can exist on the same computer or share information through the network. Generally, a client is a PC running the front-end software and knows how to communicate with the server. The server is a machine that receives request information and takes corresponding lines. As the client and server share the processing load, the overall system performance is greatly improved. In practical application, the C language or C ++ program of VC is used as the front-end client program. It receives data information from the Matlab Engine and transmits command data information with the engine. Table 1 is a list of commonly used Matlab Engine call functions.
Table 1: Matlab Engine commonly used call FunctionsUsing the Matlab Engine, you can complete the following tasks: first, you can use MATLAB as a powerful and programmable mathematical function library, and call a large number of mathematical computing functions in MATLAB to complete complex computing tasks; secondly, a complete system can be built for a specific task. The front-end user interface is written in advanced languages, and the background computing tasks are completed by the Matlab Engine. Finally, the user can not only call the Matlab Engine on the local computer, but also call the Matlab Engine on other computers through the network, which can achieve good resource sharing and job division and cooperation.
3. steps for implementing mixed programming of VC and Matlab using the Matlab EngineTo use the Matlab Engine to implement mixed programming of VC and Matlab in the VC integrated environment, follow these steps: (1) set the compiling environment of Visual C ++ 6.0: choose tools/options from the menu to open the Visual C ++ 6.0 setting properties page, go to the directorie page, select include files from the show directories for drop-down list, and add path: D: \ MATLAB 6p5 \ extern \ include (in the installation path of MATLAB); then select library files from the drop-down list and add the path: D: \ MATLAB 6p5 \ extern \ Lib \ Win32 \ Microsoft \ msvc60 (in the installation path of MATLAB ). You only need to set the compiling environment once. (2) Add a library file: Enter the link page through menu projects | settings. In the object/library modules box, add the library file name: libmx. Lib, libmat. Lib, and libeng. Lib. Each new project file must be added with a separate library file. (3) The program header file using the engine function contains the following two header files: # include "engine. H"; # include. (4) Add the program code that calls the Matlab Engine in the application:
{Engine * EP; If (! (Ep = engopen ("\ 0") // open the Matlab Engine and establish a connection with the local MATLAB {fprintf (stderr, "\ n can't start Matlab Engine \ n"); exit (-1 );} |
The above steps are used to set the compiling environment of VC and Matlab Mixed Programming. After completing the above basic steps, you can use the Matlab Engine library function engevalstring to call the built-in functions of Matlab.
4. Examples of VC and Matlab hybrid programming applicationsBased on the above method, the following uses a specific strength to explain how to implement the mixed programming of VC and MATLAB, the data communication between them, and the call of MATLAB commands in the VC environment. First, use the MFC of VC ++ 6.0 to create an application with a single document structure, add a menu resource, and map corresponding function functions to it. Second, based on the Matlab Engine to implement the mixed programming steps of VC and Matlab (see section 3), set the corresponding compiling environment for the program and add the connection library function. Then, call the built-in functions and commands of Matlab, the main program code is as follows:
... Static double areal [6] = {1, 2, 3, 4, 5, 6}; mxarray * t = NULL, * A = NULL, * D = NULL; double time [10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};/* use the MX function library to create a matrix and pass parameters. */T = mxcreatedoublematrix (1,10, mxreal); memcpy (char *) mxgetpr (t), (char *) time, 10 * sizeof (double )); /* pass the parameter to the server using the Matlab Engine Connection handle created above */engputvariable (Ep, "T", t ); /* Call built-in functions and commands of MATLAB */engevalstring (Ep, "d =. 5.*(-9.8 ). * t. ^ 5; "); engevalstring (Ep," plot (T, d );");... |
Finally, add the header file: # include "Matlab. H ". Compile and run the program to get the result shown in (Figure 1:
Figure 1: Call the Matlab Engine to output resultsThe above example is a simple application of Mixed Programming of VC and MATLAB. In actual numerical computing, control system development and other engineering applications, the hybrid programming technology of VC and MATLAB can be well used, this simplifies the development workload of applications.
5. ConclusionMATLAB provides powerful mathematical computing functions and rich toolboxes. Using VC and Matlab hybrid programming, you can use your own expertise to shorten the development cycle of engineering application software, it also improves the program running efficiency. Among them, the Matlab Engine Based on the client/server computing mode is a common method for implementing VC and Matlab Mixed Programming. Through the object-oriented design of engine calls and data organization methods, data organization is transparent, greatly simplifying the program design process, it encapsulates access to the Matlab Engine and data exchange details with the MATLAB server, providing users with a friendly programming interface environment. From http://developer.51cto.com/art/200701/37357.htm#commment