How MATLAB calls C/C ++ Functions

Source: Internet
Author: User
Use MATLAB to compile C/C ++ functions into Mex functions, which can be called in MATLAB.

1. First install the Compiler
In Matlab, type Mex-setup and select the compiler you want to compile C ++.

2. Write c ++ Functions
The function format must be
Void mexfunction (INT nlhs, mxarray * plhs [], int nrhs, const mxarray * prhs [])
Nlhs: Number of output parameters
Plhs: output parameter list
Nrhs: Number of input parameters
Prhs: List of input parameters
But the function name can be retrieved at will. Note: The saved file name is the name of the function called in MATLAB in the future, not the name of the function here.
The following is an example to capture some elements of an array to form a new array.
Three input parameters, target array, truncated rows (vectors), and truncated columns (vectors)
Two output parameters. The truncated array and array dimension information
The function shows how to pass in outgoing parameters, and if every parameter is retrieved from the parameter list, the conversion between MATLAB data and C ++ data, and some output functions.
Create a resizearray. cpp file (resizearray will be used as the function name called by MATLAB) and write the following code
# Include "Mex. H"
// Author: Wang bangzhu 2010.05.05
// MATLAB call method: [resizedarr, resizeddims] = resizearray (ARR, selrows, sekcols)
Void mexfunction (INT nlhs, mxarray * plhs [], int nrhs, const mxarray * prhs [])
{
If (nrhs! = 3)
{
Mexerrmsgtxt ("the number of parameters is incorrect! ");
}

Int rownum = mxgetm (prhs [0]);
Int colnum = mxgetn (prhs [0]);
Double * Parr = (double *) mxgetpr (prhs [0]);
// Obtain the selected row and column information
// Supports both row vectors and column vectors.
Double * pselrows = (double *) mxgetpr (prhs [1]);
Double * pselcols = (double *) mxgetpr (prhs [2]);
Int selrowsrownum = mxgetm (prhs [1]);
Int selrowscolnum = mxgetn (prhs [1]);
If (selrowsrownum! = 1 & selrowscolnum! = 1)
{
Mexerrmsgtxt ("the row parameter is incorrect! ");
}
Int selrowsnum = selrowsrownum * selrowscolnum;


Int selcolsrownum = mxgetm (prhs [2]);
Int selcolscolnum = mxgetn (prhs [2]);
If (selcolsrownum! = 1 & selcolscolnum! = 1)
{
Mexerrmsgtxt ("the column parameter is incorrect! ");
}
Int selcolsnum = selcolsrownum * selcolscolnum;

Plhs [1] = mxcreatedoublematrix (2, 1, mxreal );
Double * resizeddims = (double *) mxgetpr (plhs [1]);
Resizeddims [0] = selrowsnum;
Resizeddims [1] = selcolsnum;

Plhs [0] = mxcreatedoublematrix (selrowsnum, selcolsnum, mxreal );
Double * presizedarr = (double *) mxgetpr (plhs [0]);

// Here, the data in MATLAB must be column-based.
# Define Arr (row, col) Parr [(COL) * rownum + row]
# Define rarr (row, col) presizedarr [(COL) * selrowsnum + row]
For (INT rI = 0; RI <selrowsnum; ri ++)
{
For (int ci = 0; CI <selcolsnum; CI ++)
{
Rarr (Ri, CI) = Arr (INT) pselrows [ri]-1, (INT) pselcols [CI]-1 );
}
}

Mexico printf ("OK! /N ");
}

3. Compile the C ++ function as the Mex function.
Put resizearray. cpp in the current MATLAB directory, and enter Mex resizearray. cpp in MATLAB. After compilation is successful, resizearray. Mexico W32 will be generated.

4. Call a function
Arr = [; 31: 39; 41: 49; 51: 59; 61: 69];
Selrows = [1 3];
Selcols = [2: 4 5 9];
[Rarr, rdims] = resizearray (ARR, rows, cols );
Data in arr:
11 12 13 14 15 16 17 18 19
21 22 23 24 25 26 27 28 29
31 32 33 34 35 36 37 38 39
41 42 43 44 45 46 47 48 49
51 52 53 54 55 56 57 58 59
61 62 63 64 65 66 67 68 69
Data in rarr:
12 13 14 15 19
32 33 34 35 39
Rdims:
2
5


OK, done!

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.