最近写了个Matlab程序,好慢呐……所以开始学习Matlab与C/C++混合编程。下面写了个测试代码,显示一个Double类型矩阵中的元素。
源代码
- #include "mex.h"
- void displaySubscript( const mxArray *pArray, mwSize index );
- // 入口函数
- void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) {
- // 源文件名后缀为.c时,所有变量声明必须一次性完成,且放在最前面, 否则mex编译错误
- // 源文件名后缀为.cpp时,没有上面的问题,...- -||
- double *pData;
- mwSize i;
- // 输入参数必须为一个,且为double类型
- if ( nrhs != 1 || mxDOUBLE_CLASS != mxGetClassID(prhs[0]) ) {
- mexErrMsgTxt( "输入参数不合法……" );
- }
- // 获取数据指针
- pData = mxGetPr(prhs[0]);
- // 遍历所有元素并打印到屏幕
- for ( i = 0; i != mxGetNumberOfElements(prhs[0]); i++ ) {
- displaySubscript( prhs[0], i );
- mexPrintf( " = %g/n", pData[i] );
- }
- }
- void displaySubscript( const mxArray *pArray, mwSize index ) {
- // 源文件名后缀为.c时,所有变量声明必须一次性完成,且放在最前面, 否则mex编译错误
- // 源文件名后缀为.cpp时,没有上面的问题,...- -||,代码好龊...
- mwSize i, j;
- mwSize numOfDim;
- mwSize *subScript;
- mwSize subIndex;
- mwSize total;
- const mwSize *Dims;
- const char *className;
- // 获取维度个数
- numOfDim = mxGetNumberOfDimensions(pArray);
- // 获取维度数组
- Dims = mxGetDimensions(pArray);
- // 获取类型名称
- className = mxGetClassName(pArray);
- // 分配下标数组内存
- subScript = (mwSize *)mxCalloc( numOfDim, sizeof( mwSize ) );
- // 根据当前的索引号生成下标
- subIndex = index;
- for ( i = numOfDim - 1; ; i-- ) {
- total = 1;
- for ( j = 0; j < i; j++ ) {
- total *= Dims[j];
- }
- subScript[i] = subIndex / total;
- subIndex = subIndex % total;
- if ( 0 == i ) {
- break;
- }
- }
- // 打印出所有下标
- mexPrintf( "(" );
- for ( i = 0; i < numOfDim - 1; i++ ) {
- mexPrintf( "%d,", subScript[i] + 1 );
- }
- mexPrintf( "%d)", subScript[numOfDim-1] + 1 );
- // 释放下标数组内存
- mxFree( subScript );
- }
在Matlab使用mex命令编译源文件时,要注意这样一个现象:源文件名后缀为.c时,所有变量声明必须一次性完成,且放在最前面, 否则mex编译错误;而源文件名后缀为.cpp时,就没有上面的问题,...- -||。
实验结果
混合编程API一览
MX Matrix Library
Type for index values | |
Pointer type for platform | |
Signed integer type for size values | |
Type for size values | |
Field to structure array | |
Type for MATLAB array | |
Convert array to string | |
Check assertion value for debugging purposes | |
Check assertion value without printing assertion text | |
Offset from first element to desired element | |
Allocate dynamic memory for array using MATLAB memory manager | |
Type for string array | |
Enumerated value identifying class of array | |
Identifier corresponding to class | |
Flag specifying whether array has imaginary components | |
CHARACTER values from Fortran array to pointer array | |
COMPLEX*16 values from Fortran array to pointer array | |
COMPLEX*8 values from Fortran array to pointer array | |
INTEGER*1 values from Fortran array to pointer array | |
INTEGER*2 values from Fortran array to pointer array | |
INTEGER*4 values from Fortran array to pointer array | |
CHARACTER values from pointer array to Fortran array | |
COMPLEX*16 values from pointer array to Fortran array | |
COMPLEX*8 values from pointer array to Fortran array | |
INTEGER*1 values from pointer array to Fortran array | |
INTEGER*2 values from pointer array to Fortran array | |
INTEGER*4 values from pointer array to Fortran array | |
Pointer values from pointer array to Fortran array | |
REAL*4 values from pointer array to Fortran array | |
REAL*8 values from pointer array to Fortran array | |
REAL*4 values from Fortran array to pointer array | |
REAL*8 values from Fortran array to pointer array | |
Unpopulated N-D cell array | |
Unpopulated 2-D cell array | |
Unpopulated N-D string array | |
Create populated 2-D string array | |
2-D, double-precision, floating-point array initialized to 0 | |
Scalar, double-precision array initialized to specified value | |
N-D logical array initialized to false | |
2-D, logical array initialized to false | |
Scalar, logical array | |
Unpopulated N-D numeric array | |
Numeric matrix initialized to 0 | |
2-D unpopulated sparse array | |
Unpopulated 2-D, sparse, logical array | |
Create 1-by-N array initialized to specified string | |
Unpopulated N-D structure array | |
Unpopulated 2-D structure array | |
Free dynamic memory allocated by MXCREATE* functions | |
Make deep copy of array | |
Free dynamic memory allocated by MXCALLOC, MXMALLOC, or MXREALLOC functions | |
Contents of array cell | |
Pointer to character array data | |
Class of array | |
Class of array as string | |
Pointer to real data | |
Pointer to dimensions array | |
Number of bytes required to store each data element | |
Value of EPS | |
Field value, given field name and index, into structure array | |
Field value, given field number and index, into structure array | |
Field name, given field number, in structure array | |
Field number, given field name, in structure array | |
Pointer to imaginary data of array | |
Value of infinity | |
Sparse matrix IR array | |
Sparse matrix JC array | |
Pointer to logical array data | |
Number of rows in array | |
Number of columns in array | |
Value of NaN (Not-a-Number) | |
Number of dimensions in array | |
Number of elements in array | |
Number of fields in structure array | |
Number of elements in IR, PR, and PI arrays | |
Imaginary data elements in array of type DOUBLE | |
Real data elements in array of type DOUBLE | |
Value of public property of MATLAB object | |
Real component of first data element in array | |
String array to C-style string | |
Determine whether input is cell array | |
Determine whether input is string array | |
Determine whether array is member of specified class | |
Determine whether data is complex | |
Determine whether mxArray represents data as double-precision, floating-point numbers | |
Determine whether array is empty | |
Determine whether input is finite | |
Determine whether array was copied from MATLAB global workspace | |
Determine whether input is infinite | |
Determine whether array represents data as signed 16-bit integers | |
Determine whether array represents data as signed 32-bit integers | |
Determine whether array represents data as signed 64-bit integers | |
Determine whether array represents data as signed 8-bit integers | |
Determine whether array is of type mxLogical | |
Determine whether scalar array is of type mxLogical | |
Determine whether scalar array of type mxLogical is true | |
Determine whether input is NaN (Not-a-Number) | |
Determine whether array is numeric | |
Determine whether array represents data as single-precision, floating-point numbers | |
Determine whether input is sparse array | |
Determine whether input is structure array | |
Determine whether array represents data as unsigned 16-bit integers | |
Determine whether array represents data as unsigned 32-bit integers | |
Determine whether array represents data as unsigned 64-bit integers | |
Determine whether array represents data as unsigned 8-bit integers | |
Type for logical array | |
Allocate dynamic memory using MATLAB memory manager | |
Reallocate dynamic memory using MATLAB memory manager | |
Remove field from structure array | |
Value of one cell of array | |
Convert structure array to MATLAB object array | |
Set pointer to data | |
Modify number of dimensions and size of each dimension | |
Set structure array field, given structure field name and array index | |
Set structure array field, given field number and index | |
Imaginary data pointer for array | |
IR array of sparse array | |
JC array of sparse array | |
Number of rows in array | |
Set number of columns in array | |
Set storage space for nonzero elements | |
Set new imaginary data for array | |
Set new real data for array | |
Set value of public property of MATLAB object |
MEX Library
Register function to call when MEX-function cleared or MATLAB software terminates | |
Call MATLAB function, user-defined function, or MEX-file | |
Call MATLAB function, user-defined function, or MEX-file and capture error information | |
Display error message with identifier and return to MATLAB prompt | |
Display error message and return to MATLAB prompt | |
Execute MATLAB command in caller workspace | |
Execute MATLAB command in caller workspace and capture error information | |
Entry point to C/C++ or Fortran MEX-file | |
Name of current MEX-function | |
Value of specified Handle Graphics property | |
Copy of variable from specified workspace | |
Read-only pointer to variable from another workspace | |
Determine whether variable has global scope | |
Determine whether MEX-file is locked | |
Prevent clearing MEX-file from memory | |
Make array persist after MEX-file completes | |
Make memory allocated by MATLAB software persist after MEX-function completes | |
ANSI C PRINTF-style output routine | |
Array from MEX-function into specified workspace | |
Set value of specified Handle Graphics property | |
Control response of MEXCALLMATLAB to errors | |
Allow clearing MEX-file from memory | |
Warning message with identifier | |
Warning message |