A preliminary study on mixed programming of C + + and MATLAB

Source: Internet
Author: User

==================================================================

% Welcome Reprint, Respect original, so reprint please indicate the source.

% http://blog.csdn.net/bendanban/article/details/37830495

======================= Split line below for text ================================

Matlab has a wealth of functions, simple programming. However, in some cases, MATLAB program execution speed is relatively slow. C + + Compile the execution of the program faster, programming difficulty than the MATLAB a bit higher. Therefore, there is a solution, that is, using MATLAB to implement our experimental program, using C/s to achieve the MATLAB program in the more time-consuming parts, from the MATLAB program to call the C + + program to achieve acceleration.

This article mainly explains how to call the C + + program in MATLAB, as well as how to write the MATLAB program can be invoked by the program.

This article is mainly divided into the following parts:

Hello matlab, with a simple example to illustrate how to call in the MATLAB procedures, and the MATLAB can be invoked in C + + program should pay attention to the basic issues.

MATLAB calls the C + + program pass parameters. Discusses how to use the parameters of Matlab in C/s + +.

The methodology of the mixed programming of MATLAB and C + +. This paper gives a way to use MATLAB and C/A + + programming in general, so that we can have a clearer idea of applying this technique.

A description of the data store. Description of the data in the MATLAB storage mode.

Note: This article thinks that the reader will use MATLAB, Master C + + language, and have a computer. The operating system with Windows installed on the computer has MATLAB and visual Studio (such as vs2008,vs2010, etc.) on the operating system. Or a Linux operating system installed on the computer, with MATLAB,GCC on the system.


Hello Matlab

We step by step to complete a call "Hello Matlab" program.

The first step: under your computer D disk, create a directory named Himat. Create a text file in the D:\HiMat directory named "Abhimat.cpp". Copy the code in 1 to the "abhimat.cpp" File and save it. ( note that the creation of a directory and naming act here is not a rule, just for ease of explanation).

#include "mex.h"
void mexfunction (int nlhs, Mxarray *plhs[], int nrhs, const mxarray *prhs[])
{
  mexprintf ("H Ello matlab in c/cpp.\n ");
}
Code 1, Abhimat.cpp

"Abhimat.cpp" is to be called in the MATLAB C + + code.

Step two: Compile "abhimat.cpp" in MATLAB. Start Matlab, enter the D:\HiMat directory, in the MATLAB command window to execute the commands in code 2, according to the prompts to complete the configuration of the C + + compilation environment. Note : If you have multiple compilers, it is recommended that you select the latest.

Mex-setup
Code 2, set up matlab C + + compiler environment

After the configuration is completed, execute code 3 command in MATLAB to compile abhimat.cpp.

Mex Abhimat.cpp
Code 3, compiling abhimat.cpp

Step three: Execute the compiled C/C + + program. In the MATLAB command Window input "Abhimat" or "Abhimat ()", can call the compiled program, recommend the use of the latter.

The attentive reader has noticed:

In Matlab, the name of a/C + + function called is the filename that is compiled to mex* (here, * represents any number of characters, such as mexw64) as the suffix name. MATLAB executes the Abhimat () command, the actual execution is the program in the Mexfunction function.

MATLAB calls the C + + program pass parameters


This section we discuss, in MATLAB for the call of C/s + + program, we are how to know the parameters of Matlab call the type, number of.

This paper gives an example of a call to a C + + program in MATLAB, as shown in code 4.

c = [1 2;3 4;5 6];
d = [1 1;1 1;1 1];
[A, b] = Abfunc (c, D);
Code 4, MATLAB calls C + + program instance

The following work is how to implement the Mexfunction function in the next file named Abfunc.cpp in the current directory. In this function how to obtain the MATLAB command in the C, D two variables of the value, how to return a, b two variables.

Note that the four parameters in the Mexfunction function are described:

The first parameter of the Nlhs:mexfunction, which indicates that there are several variables to the left of the equals sign in the Invoke command of MATLAB. For example, the call in code 4, the value of NLHS is 2, because it has two variables to the left of the equals sign, they are A and B.

Plhs:mexfunction's second parameter, which indicates the pointer to the left variable of the equals sign in the invocation command of MATLAB. For example, the call in code 4, Plhs[0], indicates that a,plhs[1] represents B.

Nrhs:mexfunction's third parameter, which indicates the number of variables to the right of the equals sign in the invocation command of MATLAB. For example, the call in code 4, the value of NRHS is 2, because it has two variables on the right side of the equals sign, they are C and D.

The fourth parameter of the Prhs:mexfunction, which indicates the variable pointer to the right of the equals sign in the matlab invocation command. For example, the call in code 4, Prhs[0], indicates that c,prhs[1] represents D.

Mxarrary is an invisible data type, is defined by MATLAB, we just need to know mxarrary pointer and matlab in the variable one by one corresponding on it.

the following implementation abfunc.cpp, the function is a=c+d b = c-d;

#include "mex.h" void mexfunction (int nlhs, Mxarray *plhs[], int nrhs, const Mxarray *prhs[]) {double *p_c, *p_d;

  Double *p_a, *p_b;
  int c_rows, C_cols;
  
  int d_rows, D_cols;
  int Numel;
  
  int n;

  Mxassert (nlhs==2 && nrhs==2, "Error:number of Variables"); C_rows = Mxgetm (Prhs[0])//Get rows of c C_cols = MXGETN (prhs[0));/get cols of C d_rows = Mxgetm (prhs[1));/Get RO  WS of D d_cols = Mxgetn (prhs[1])//Get cols of D Mxassert (c_rows==d_rows && c_cols==d_cols, "Error:cols and

  Rows ");
  Create output buffer plhs[0] = Mxcreatedoublematrix (C_rows, C_cols, mxreal);

  PLHS[1] = Mxcreatedoublematrix (C_rows, C_cols, mxreal);
  Get buffer pointers p_a = (double*) mxgetdata (plhs[0));
  P_b = (double*) mxgetdata (plhs[1]);
  P_c = (double*) mxgetdata (Prhs[0]);

  P_d = (double*) mxgetdata (prhs[1]); Compute a = c + D;
  b = c-d;
  Numel = C_rows*c_cols;
    for (n = 0; n < numel n++) {P_a[n] = P_c[n] + p_d[n]; P_b[n] = p_c[N]-p_d[n]; }
}
Code 5, implementation of Abfunc.cpp

Explain the functions that are used in code 5. Most of these functions begin with MX. Mxassert is an assertion, similar to an assert in c\c++. Mxgetm get the number of variables from MATLAB, MXGETN get the number of variables from MATLAB. Mxcreatedoublematrix creates a 2-dimensional MATLAB variable, which is used to specify the number of rows, columns, and element types (Mxreal represents real numbers, Mxcomplex represent complex numbers), respectively. Mxgetdata is used to obtain the first address of an in-memory block of data.

Compile and test the code in 5, and refer to the Codes 6.

Mex Abfunc.cpp
c = [1 2;3 4;5 6];
d = [1 1;1 1;1 1];
[A, b] = Abfunc (c, D);
Code 6, CODE5 test codes

The output of Code 6 is as follows:

A =
     2     3
     4     5
     6     7
b =
     0     1 2 3-4 5



a description of the data store

The data in MATLAB is stored by columns. For example, the order in which a=[1,2;3,4;5,6],a data is stored in memory is: 1, 3, 5, 2, 4, 6. When using the variables from Matlab in c\c++, be sure to pay attention to the storage order of the data .


the methodology of mixed programming between MATLAB and C + +

In view of the @ programming small hand suggestion, add a methodological flowchart about using this method. Hopefully it will give you an idea of the circumstances under which you can use this hybrid programming technique and how to achieve it step-by-step. See flowchart in Figure 1:

Fig. 1, MATLAB and C + + mixed methodology flow chart

Additional: Text in the code download location: http://download.csdn.net/detail/bendanban/7643701

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.