Matlab & C++ 混合程式設計mex檔案的編寫與除錯

thuwangxiang10發表於2015-08-05

在編寫Matlab程式的時候,我們經常需要呼叫C++語言編寫的程式以提高演算法執行速度等。本文將介紹怎樣編寫及編譯mex檔案。通俗的講,Matlab與C++的混合程式設計就是,Matlab將輸入變數傳給C++,C++執行完後將結果返回給Matlab。因此,編寫mex檔案的關鍵就在於輸入輸出的處理,其他均與C++無異。

mex檔案的編寫

mex 函式的編寫關鍵在於入口函式及返回值。所有引數均通過指標傳遞。cpp檔名即為Matlab呼叫的函式名。cpp檔案需要

#include "mex.h"

入口函式的規範:

void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) 

// nlhs:輸出引數個數
// plhs:輸出引數列表
// nrhs:輸入引數個數
// prhs:輸入引數列表

引數傳入舉例:

double *pint= (double*)mxGetData(prhs[0]);
int a = (int)pint[0];

這樣即可將第一個變數以 int 型傳入到num_vertices 變數。
同時可以加入引數個數判斷,以防止錯誤:

if (nrhs != 3)
    {
        mexErrMsgTxt( "引數錯誤!請檢查引數個數" );
    }

引數傳入後就可以按照C++的方法去實現演算法,最終將演算法執行後的結果傳出到Matlab中。

引數傳出:

  • 首先用傳出引數列表plhs申請空間,如
   plhs[0] = mxCreateDoubleMatrix(u->num, 1, mxREAL);
   plhs[1] = mxCreateDoubleMatrix(u->num, 1, mxREAL);
   plhs[2] = mxCreateDoubleMatrix(u->num, 1, mxREAL);
  • 隨後取得以上引數的地址,如
   double* a=(double *)mxGetPr(plhs[0]);
   double* b=(double *)mxGetPr(plhs[1]);
   double* c=(double *)mxGetPr(plhs[2]);

這樣將將變數a,b,c的結果傳出到mex的輸出變數了。

編譯:

在Matlab中 mex xxx.cpp 即可。

mex檔案的除錯

參見部落格.

附錄:

混合程式設計API一覽,參見Mtahworks網站

mwIndex (C and Fortran) Type for index values
mwPointer (Fortran) Pointer type for platform
mwSignedIndex (C and Fortran) Signed integer type for size values
mwSize (C and Fortran) Type for size values
mxAddField (C and Fortran) Field to structure array
mxArray (C and Fortran) Type for MATLAB array
mxArrayToString (C) Convert array to string
mxAssert (C) Check assertion value for debugging purposes
mxAssertS (C) Check assertion value without printing assertion text
mxCalcSingleSubscript (C and Fortran) Offset from first element to desired element
mxCalloc (C and Fortran) Allocate dynamic memory for array using MATLAB memory manager
mxChar (C) Type for string array
mxClassID (C) Enumerated value identifying class of array
mxClassIDFromClassName (Fortran) Identifier corresponding to class
mxComplexity (C) Flag specifying whether array has imaginary components
mxCopyCharacterToPtr (Fortran) CHARACTER values from Fortran array to pointer array
mxCopyComplex16ToPtr (Fortran) COMPLEX*16 values from Fortran array to pointer array
mxCopyComplex8ToPtr (Fortran) COMPLEX*8 values from Fortran array to pointer array
mxCopyInteger1ToPtr (Fortran) INTEGER*1 values from Fortran array to pointer array
mxCopyInteger2ToPtr (Fortran) INTEGER*2 values from Fortran array to pointer array
mxCopyInteger4ToPtr (Fortran) INTEGER*4 values from Fortran array to pointer array
mxCopyPtrToCharacter (Fortran) CHARACTER values from pointer array to Fortran array
mxCopyPtrToComplex16 (Fortran) COMPLEX*16 values from pointer array to Fortran array
mxCopyPtrToComplex8 (Fortran) COMPLEX*8 values from pointer array to Fortran array
mxCopyPtrToInteger1 (Fortran) INTEGER*1 values from pointer array to Fortran array
mxCopyPtrToInteger2 (Fortran) INTEGER*2 values from pointer array to Fortran array
mxCopyPtrToInteger4 (Fortran) INTEGER*4 values from pointer array to Fortran array
mxCopyPtrToPtrArray (Fortran) Pointer values from pointer array to Fortran array
mxCopyPtrToReal4 (Fortran) REAL*4 values from pointer array to Fortran array
mxCopyPtrToReal8 (Fortran) REAL*8 values from pointer array to Fortran array
mxCopyReal4ToPtr (Fortran) REAL*4 values from Fortran array to pointer array
mxCopyReal8ToPtr (Fortran) REAL*8 values from Fortran array to pointer array
mxCreateCellArray (C and Fortran) Unpopulated N-D cell array
mxCreateCellMatrix (C and Fortran) Unpopulated 2-D cell array
mxCreateCharArray (C and Fortran) Unpopulated N-D string array
mxCreateCharMatrixFromStrings (C and Fortran) Create populated 2-D string array
mxCreateDoubleMatrix (C and Fortran) 2-D, double-precision, floating-point array initialized to 0
mxCreateDoubleScalar (C and Fortran) Scalar, double-precision array initialized to specified value
mxCreateLogicalArray (C) N-D logical array initialized to false
mxCreateLogicalMatrix (C) 2-D, logical array initialized to false
mxCreateLogicalScalar (C) Scalar, logical array
mxCreateNumericArray (C and Fortran) Unpopulated N-D numeric array
mxCreateNumericMatrix (C and Fortran) Numeric matrix initialized to 0
mxCreateSparse (C and Fortran) 2-D unpopulated sparse array
mxCreateSparseLogicalMatrix (C) Unpopulated 2-D, sparse, logical array
mxCreateString (C and Fortran) Create 1-by-N array initialized to specified string
mxCreateStructArray (C and Fortran) Unpopulated N-D structure array
mxCreateStructMatrix (C and Fortran) Unpopulated 2-D structure array
mxDestroyArray (C and Fortran) Free dynamic memory allocated by MXCREATE* functions
mxDuplicateArray (C and Fortran) Make deep copy of array
mxFree (C and Fortran) Free dynamic memory allocated by MXCALLOC, MXMALLOC, or MXREALLOC functions
mxGetCell (C and Fortran) Contents of array cell
mxGetChars (C) Pointer to character array data
mxGetClassID (C and Fortran) Class of array
mxGetClassName (C and Fortran) Class of array as string
mxGetData (C and Fortran) Pointer to real data
mxGetDimensions (C and Fortran) Pointer to dimensions array
mxGetElementSize (C and Fortran) Number of bytes required to store each data element
mxGetEps (C and Fortran) Value of EPS
mxGetField (C and Fortran) Field value, given field name and index, into structure array
mxGetFieldByNumber (C and Fortran) Field value, given field number and index, into structure array
mxGetFieldNameByNumber (C and Fortran) Field name, given field number, in structure array
mxGetFieldNumber (C and Fortran) Field number, given field name, in structure array
mxGetImagData (C and Fortran) Pointer to imaginary data of array
mxGetInf (C and Fortran) Value of infinity
mxGetIr (C and Fortran) Sparse matrix IR array
mxGetJc (C and Fortran) Sparse matrix JC array
mxGetLogicals (C) Pointer to logical array data
mxGetM (C and Fortran) Number of rows in array
mxGetN (C and Fortran) Number of columns in array
mxGetNaN (C and Fortran) Value of NaN (Not-a-Number)
mxGetNumberOfDimensions (C and Fortran) Number of dimensions in array
mxGetNumberOfElements (C and Fortran) Number of elements in array
mxGetNumberOfFields (C and Fortran) Number of fields in structure array
mxGetNzmax (C and Fortran) Number of elements in IR, PR, and PI arrays
mxGetPi (C and Fortran) Imaginary data elements in array of type DOUBLE
mxGetPr (C and Fortran) Real data elements in array of type DOUBLE
mxGetProperty (C and Fortran) Value of public property of MATLAB object
mxGetScalar (C and Fortran) Real component of first data element in array
mxGetString (C and Fortran) String array to C-style string
mxIsCell (C and Fortran) Determine whether input is cell array
mxIsChar (C and Fortran) Determine whether input is string array
mxIsClass (C and Fortran) Determine whether array is member of specified class
mxIsComplex (C and Fortran) Determine whether data is complex
mxIsDouble (C and Fortran) Determine whether mxArray represents data as double-precision, floating-point numbers
mxIsEmpty (C and Fortran) Determine whether array is empty
mxIsFinite (C and Fortran) Determine whether input is finite
mxIsFromGlobalWS (C and Fortran) Determine whether array was copied from MATLAB global workspace
mxIsInf (C and Fortran) Determine whether input is infinite
mxIsInt16 (C and Fortran) Determine whether array represents data as signed 16-bit integers
mxIsInt32 (C and Fortran) Determine whether array represents data as signed 32-bit integers
mxIsInt64 (C and Fortran) Determine whether array represents data as signed 64-bit integers
mxIsInt8 (C and Fortran) Determine whether array represents data as signed 8-bit integers
mxIsLogical (C and Fortran) Determine whether array is of type mxLogical
mxIsLogicalScalar (C) Determine whether scalar array is of type mxLogical
mxIsLogicalScalarTrue (C) Determine whether scalar array of type mxLogical is true
mxIsNaN (C and Fortran) Determine whether input is NaN (Not-a-Number)
mxIsNumeric (C and Fortran) Determine whether array is numeric
mxIsSingle (C and Fortran) Determine whether array represents data as single-precision, floating-point numbers
mxIsSparse (C and Fortran) Determine whether input is sparse array
mxIsStruct (C and Fortran) Determine whether input is structure array
mxIsUint16 (C and Fortran) Determine whether array represents data as unsigned 16-bit integers
mxIsUint32 (C and Fortran) Determine whether array represents data as unsigned 32-bit integers
mxIsUint64 (C and Fortran) Determine whether array represents data as unsigned 64-bit integers
mxIsUint8 (C and Fortran) Determine whether array represents data as unsigned 8-bit integers
mxLogical (C) Type for logical array
mxMalloc (C and Fortran) Allocate dynamic memory using MATLAB memory manager
mxRealloc (C and Fortran) Reallocate dynamic memory using MATLAB memory manager
mxRemoveField (C and Fortran) Remove field from structure array
mxSetCell (C and Fortran) Value of one cell of array
mxSetClassName (C) Convert structure array to MATLAB object array
mxSetData (C and Fortran) Set pointer to data
mxSetDimensions (C and Fortran) Modify number of dimensions and size of each dimension
mxSetField (C and Fortran) Set structure array field, given structure field name and array index
mxSetFieldByNumber (C and Fortran) Set structure array field, given field number and index
mxSetImagData (C and Fortran) Imaginary data pointer for array
mxSetIr (C and Fortran) IR array of sparse array
mxSetJc (C and Fortran) JC array of sparse array
mxSetM (C and Fortran) Number of rows in array
mxSetN (C and Fortran) Set number of columns in array
mxSetNzmax (C and Fortran) Set storage space for nonzero elements
mxSetPi (C and Fortran) Set new imaginary data for array
mxSetPr (C and Fortran) Set new real data for array
mxSetProperty (C and Fortran) Set value of public property of MATLAB object

相關文章