C + + Builder call matlab

Source: Internet
Author: User
Tags mixed

Borland C++builder is a novel visual programming language. In the engineering application, we generally use the C++builder language to write the application, realizes the interactive interface, the data acquisition and the port operation and so on, but c++builder in the numerical processing analysis and the algorithm tool and so on aspect, its efficiency is far lower than MATLAB language. In the accurate and convenient rendering of data graphics, MATLAB language has incomparable advantages. In addition, MATLAB also provides a powerful toolbox. However, the disadvantage of MATLAB is not to achieve port operation and real-time control. Therefore, if the combination of the two can be used to achieve complementary advantages, will obtain great benefits.

This paper introduces the method of invoking MATLAB in WinDOS application Borland c++builder3.0 developed by combining with practice.

  One, the c++builder call MATLAB realization plan

1. Realize the idea

In the high version of MATLAB (such as Matlab V4.2) provides a DDE interface, users can use the Windows DDE Communication Base system to implement external calls. This implementation is relatively simple, but will increase the main program code, affecting the speed of operation.

In Windows systems, DLLs are a very special executable file that can be accessed simultaneously by multiple Windows applications with a fixed shared data segment. The data segment data is kept in memory until the DLL is downloaded by Windows, so you can implement the data transfer and function call between the user program and MATLAB through the DLL.

Specifically, the use of MATLAB 32-bit Dynamic connection library (DLL), to generate the corresponding can be called C++builder DLL, to provide a basic support environment between the two. You can share data segments by simply loading the DLL in the user program. Then in the user program to manipulate the data section of the DLL, and through a certain way in the user program in the MATLAB to execute the DLL, you can implement the user program to the MATLAB call. The form can be mixed programming or function calls, very convenient and efficient.

2. How to Achieve

MATLAB provides the external connection of the DLL file, by converting it to the corresponding Lib file, and the necessary settings, you can directly in the C++builder MATLAB function call, the realization of C + + Builder language and MATLAB language mixed programming.

(1) Operating environment requirements

Because MATLAB provides a 32-bit DLL. Its operating environment requirements are MATLAB V4.2 or later version. C++builder can do 32-bit programming, and here we are using the V3.0 version.

(2) C++builder The generation of the Lib file

The def file provided by Matlab allows the user to generate the corresponding LIB file through the implib command. Its command format is Implib???. Lib???. def in the <matlab>externinclude directory, provides the following three. DEF file:

_libeng.def,_libmat.def,_libmx.def

The above command allows you to generate the corresponding three lib files. These LIB bases contain the necessary information for MATLAB functions that can be invoked externally.

  II. implementation calculation and drawing

For clarity, it is explained by a simple cbuilder routine. This example demonstrates C++builder's call to MATLAB by calling Matlab to realize matrix operation and drawing graph.

In the C++builder editing environment, create a new form MyForm, and put a button demo. The project file is named TRY.PRJ and its main function is try.cpp. In the main function, we will use a MATLAB call to implement the Demomatlab, as a button demo response event. The source code is as follows: #include <vcl.h>
#pragma hdrstop
#include "Unit1.h"
#pragma package (smart_init)
#pragma resource "*.DFM"
Tmyform *myform;
__fastcall tmyform::tmyform (tcomponent* Owner)
: Tform (Owner)
{
}
void __fastcall tmyform::D emoclick (TObject *sender)
{
Demomatlab (); Demo MATLAB Language call
}

  
In order to invoke the functions in MATLAB, you must make the necessary settings to add the files containing these functions to the engineering file TRY.PRJ. The following is an operation procedure:

1. Add Engine.h to the header file. It contains function declarations that initiate MATLAB calls and shutdowns.

2. Open project| Option ... dialog box, click Directories/conditionals.

· In include path, add the directory path <matlab>externinclude, which contains useful header files such as Engine.h and Matlab.h.

· In the library path, add <matlab>bin and <matlab>externinclude. The two directory paths contain DLLs and Lib files that can be invoked externally.

3. Click to select Project| Add to Project ... dialog box, add the following library file:

_libeng.lib,_libmat.lib and _libmx.lib.

Once these necessary settings are made, we can use the appropriate functions to achieve the goal. The following is the program code for the child function Demomatlab. void Demomatlab
{
Engine *eng;
//define MATLAB engine
Char buffer[200];//define Data buffer
int array[6]={1,2,3,4,5,6};< br> Mxarray *s = null, *t = NULL;
Engopen (NULL);//Open MATLAB engine---1
s= mxcreatedoublematrix (1,6, mxreal);
//Generate matrix variable
Mxsetname (s, "s");
memcpy ((char*) MXGETPR (S), (char *) array, 6*sizeof (int));
Engputarray (Eng, S);///place variable x into working space in MATLAB
Engevalstring (eng, "T = s/s.^2;");//COMPUTE
Engevalstring (Eng, "Plot" (s , T); "); Draw a graphic
.....
Engoutputbuffer (Eng, buffer, 200);//get MATLAB output

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.