标签:des style blog http color 使用 os strong
由于看的代码里面以及一些工程需要涉及一些Matlab 混合编程,一直想看一下,首先一个就是使用C编写代码,编译之后由Matlab调用,这需要用到Mex函数,mex程序就是根据一定的接口规范(mtlab提出的)编写的一个dll,Mex文件既可以用c,也可以用fortran来编。本文介绍的是用C编写的。
这样做,若是代码中有循环的话,使用matlab的话需要循环解释多次,而使用C语言编译成dll之后,首先可以将循环体放入二进制程序中,利用matlab加快运算,而不是循环解释执行matlab代码。还有就是使用C可以加快代码的开发效率。
#include "mex.h" void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { mexPrintf("hello,world!\n"); }
//hello.c 2.0 #include "mex.h" void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { int i; i=mxGetScalar(prhs[0]); if(i==1) mexPrintf("hello,world!\n"); else mexPrintf("大家好!\n"); }
//hello.c 2.1 #include "mex.h" void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { int *i; i=mxGetPr(prhs[0]); //prhs是一个指针数组(每一个是一个指针),所以返回一个指针 if(i[0]==1) mexPrintf("hello,world!\n"); else mexPrintf("大家好!\n"); }
//show.c 1.0 #include "mex.h" #include "mex.h" void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { double *data; int M,N; int i,j; data=mxGetPr(prhs[0]); //获得指向矩阵的指针 M=mxGetM(prhs[0]); //获得矩阵的行数 N=mxGetN(prhs[0]); //获得矩阵的列数 for(i=0;i<M;i++) { for(j=0;j<N;j++) mexPrintf("%4.3f ",data[j*M+i]); mexPrintf("\n"); } }
a=1:10; b=[a;a+1]; show(a) show(b)
//reverse.c 1.0 #include "mex.h" void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { double *inData; double *outData; int M,N; int i,j; inData=mxGetPr(prhs[0]); M=mxGetM(prhs[0]); N=mxGetN(prhs[0]); plhs[0]=mxCreateDoubleMatrix(M,N,mxREAL); outData=mxGetPr(plhs[0]); for(i=0;i<M;i++) for(j=0;j<N;j++) outData[j*M+i]=inData[(N-1-j)*M+i]; }
#include "mex.h" void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { double *inData; double *outData; int M,N; //异常处理 //异常处理 if(nrhs!=1) mexErrMsgTxt("USAGE: b=reverse(a)\n"); if(!mxIsDouble(prhs[0])) mexErrMsgTxt("the Input Matrix must be double!\n"); inData=mxGetPr(prhs[0]); M=mxGetM(prhs[0]); N=mxGetN(prhs[0]); plhs[0]=mxCreateDoubleMatrix(M,N,mxREAL); outData=mxGetPr(plhs[0]); for(i=0;i<M;i++) for(j=0;j<N;j++) outData[j*M+i]=inData[(N-1-j)*M+i]; }
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 |
参考:
http://blog.sciencenet.cn/blog-620659-579885.html
http://blog.csdn.net/raodotcong/article/details/6295859
标签:des style blog http color 使用 os strong
原文地址:http://blog.csdn.net/lansatiankongxxc/article/details/38176593