使用mex和C可以加快算法的运行速度,mex文件包含一个入口函数如下:
c}%Qe7L$vIln%J0void mexFunction( int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[] )51Testing软件测试网6X{+vB7a!f7_)G
51Testing软件测试网~%h$GT)X7s Zj3K/m
入口函数有四个参数:prhs为mxArray结构体类型的指针数组,该数组的元素按顺序指向所有的输入参数;;pL0@0n8qE0
51Testing软件测试网B.NuCEM0@S?
nrhs为整数类型,它标明了输入参数的个数。51Testing软件测试网8[2A;B-~on9u-v
51Testing软件测试网7^!})E)I:P,tJ9Cy
plhs:为mxArray结构体类型的指针数组,该数组的元素按顺序指向所有的输出参数;rL4A2}ort~0
51Testing软件测试网K9R(e7@pD.xbq
nlhs:输出参数的个数+t:d{rx8E l9r9y0
51Testing软件测试网tm*v)v4t pc2i
在入口程序完成两件事情:51Testing软件测试网!x0J+A@}OP:_6`]
5N&CY?|:B0第一:从输入的mxArray结构体中获得计算完毕的数据,然后在用户的子程序中加以使用,Xz`D7~`G0
DL(pq2r/}b!C2E]0第二:用户可以将计算完毕的结果返回给一个用于输出的mxArray结构体。51Testing软件测试网N9a#Sa.t f
51Testing软件测试网$R:FL ee,R/f&}2D
mex源文件的两个组成部分可以分为两个文件也可以放在一个文件中,必须包含mex.h头文件,该头文件包含matrix.h和一些以mex开头的子函数。51Testing软件测试网-i$\*v&\S['A"vR|E^
w
51Testing软件测试网:S7xf;Gq"g*D~
下面举几个例子:u*|\X9k
H~f5Q;x0
51Testing软件测试网%]#sn3\V0A
,BS-j#v/@9d0
lz5OR+M#E%rR0#include "mex.h"51Testing软件测试网/f.AoF6kON8e%D
/* 输入参数 */[ NT\3\4y3f0
:kF!d7VIin!k051Testing软件测试网Eg`o%g-l"k7Oj1J
#define MInput prhs[0] 51Testing软件测试网m^jx$j3uCL
/* 输出参数 */:eO,J"p i+E5jO0
R,w5k$V,I5PuYN06SX2w[8~0Hta-B0#define OUTM plhs[0]ub(\yDKf:XrE0
U m q%C1N7R6?#A)@0;sQSgM)f9@)ZL0void mexFunction( int nlhs, mxArray *plhs[],
,EI.I"HC0 int nrhs, const mxArray *prhs[] )
H6E5n3zUQ0 {51Testing软件测试网D/?'bWcQQ4t2M
int i,j,N,L;51Testing软件测试网S&b"{g_ Y(@&?
double *Input,*output;
5e%h3yk'o0 51Testing软件测试网hX6y~)yF
if (nrhs < 1) 51Testing软件测试网z.N;~}4j(S;FJF
{51Testing软件测试网j&z9X"?y#q4s
mexErrMsgTxt("至少需要一个参数!");51Testing软件测试网FXg q^J#}G
}
n6|{+gk([4|0 Input= mxGetPr(MInput); /*获取矩阵*/ 51Testing软件测试网k+x@1[4Ga
N = mxGetM( MInput ); /*获取矩阵的行*/51Testing软件测试网ZQDJ^
L = mxGetN( MInput); /*获取矩阵的列*/51Testing软件测试网v;_H(b0{T.H
for( i=0;i
}6Uk{
{
_e~8X1i"D2n3i8|Ww0 for ( j=0;j
printf("%f ", Input[i+j*N]);/*若索引从1开始则变成i-1+(j-1)*N MATLAB 是按列存数据 */ 51Testing软件测试网*u%w8CB"CA|
printf("\n"); /*输出完一行,换行*/51Testing软件测试网'd0i[:c ?4d
}
KC ]gTK~'LX0 51Testing软件测试网^ w{W%U4K p-\
OUTM= mxCreateDoubleMatrix(N,L, mxREAL); /* 建立输出矩阵*/
[6aP!p*\0 output=mxGetPr(OUTM);
'Ht'p
k.OlCwu0 for( i=0;i
4EM~"v:@J,c0{
4{Q0`%\;H/I$f0 for ( j=0;j
1u7T)['PZD#q]Vn0Ad
G0 output[i+j*N]=Input[i+j*N];/*若索引从1开始则变成i-1+(j-1)*N*/
:St?SKa$zf%A0
X#ine P"J%NF0}
m8uG_bc!\$~0 }q*tj4w"S3s051Testing软件测试网!t&aVR&o
下面是字符串的操作:51Testing软件测试网
H` _,`+st5Y;l*dV
"qpJ@8\L0Z9Iz0Vo:[3Z?I$sk0JzlB|9`J^0#include "mex.h"51Testing软件测试网 x-Y&Hm}S%e
^o7q*H3ReeP.Y0void51Testing软件测试网Q-GV"?t|-RT7k(IV
mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
u|t}2u3o0xU/kx0{51Testing软件测试网z0~XSDH!u%j
char *buf;51Testing软件测试网f1Ss1Gq
mwSize buflen;51Testing软件测试网8B*Gc9k^B"vU
int status;51Testing软件测试网x;eu!T
R2JFO2y
0V
E](H*n!Qvz0(void) plhs; /* unused parameters */
"kA;`[N:CR0
7wOOeZ}mO0 /* Check for proper number of input and output arguments */
f&L$} w:cp)]F0 if (nrhs != 1) { 51Testing软件测试网!m Y6Jibs
mexErrMsgTxt("One input argument required.");
OnZ2cv*H*?M0 } 51Testing软件测试网GcDW4p*x
if (nlhs > 1) {51Testing软件测试网3L^ j8}B&c
C
mexErrMsgTxt("Too many output arguments.");51Testing软件测试网9oYzA9Qb
}
J&{v@4X7L@1^:w0
];ttQW.FR0 /* Check for proper input type */51Testing软件测试网m/xd1u)[#i V
if (!mxIsChar(prhs[0]) || (mxGetM(prhs[0]) != 1 ) ) {
.M;a:poRL0 mexErrMsgTxt("Input argument must be a string.");51Testing软件测试网R'X
m'g_2q'[6_l
}51Testing软件测试网^-A(O%iUMy
51Testing软件测试网~bx0e1tqW
/* Find out how long the input string is. Allocate enough memory
0|d$B#yw)@#s
D0 to hold the converted string. NOTE: MATLAB stores characters51Testing软件测试网-Lj,m7Yyb&bo
as 2 byte unicode ( 16 bit ASCII) on machines with multi-byte
W|n^v;y/UD0 character sets. You should use mxChar to ensure enough space
4iLN}J0 is allocated to hold the string */
KGl1^'H2C+[?0
8XSsvR/]3c0 buflen = mxGetN(prhs[0])*sizeof(mxChar)+1;51Testing软件测试网-vRg(?A/l
buf = mxMalloc(buflen);51Testing软件测试网 F/e*T#JGniT
,ld;]4hmG0 /* Copy the string data into buf. */ 51Testing软件测试网,lr7{+?+_C#u%Q'U
status = mxGetString(prhs[0], buf, buflen); 51Testing软件测试网*Z.Z6X0GuL
mexPrintf("The input string is: %s\n", buf);51Testing软件测试网C5]|0^7LP
/* NOTE: You could add your own code here to manipulate 51Testing软件测试网'Jv`8ci tc
the string */
:iNum@P%E#U0
9wEH$Z%}9ZQ0 /* When finished using the string, deallocate it. */
5U-M9YoIdoVC0 mxFree(buf);
8A.\SKN0C'kw,A0
)B?e6@!G)F+i0}Q
oa:K9`E A0
0],w8V9v-f*H0动态分配内存的例子:v)Xo%S2gsKx0
51Testing软件测试网f:]sbB'_$aA0I
51Testing软件测试网5X0J;Ef?*e"I%SSc`{f%s4`{0#include "mex.h"oB-QMF0
\M-UJVL0/* The mxArray in this example is 2x2 */51Testing软件测试网E#J5ps!_Qc
#define ROWS 2
?f~rl-X8Y&a0#define COLUMNS 251Testing软件测试网&b+T$M/yzUc
#define ELEMENTS 451Testing软件测试网gRFe6q
51Testing软件测试网&u#Y7FW:z$PI O ]!nN
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {51Testing软件测试网L4Wrq,K:{
double *pointer; /* pointer to real data in new array */51Testing软件测试网j.QmjH+X5Q
double *dynamicData; /* pointer to dynamic data */
~t.O?.eB!k#TE0 const double data[] = {2.1, 3.4, 2.3, 2.45}; /* existing data */
:q`G~,f:XgpEC0 mwSize index;51Testing软件测试网(u2z?zx)a"jC
51Testing软件测试网Q-E@4A#g~"}X
/* Check for proper number of arguments. */
?#PP'koV0 if ( nrhs != 0 ) {
\#KR6Hl@a8@0 mexErrMsgIdAndTxt("MATLAB:arrayFillGetPrDynamicData:rhs","This function takes no input arguments.");51Testing软件测试网V9]/x};f`?\
}51Testing软件测试网{&vp G3C3gY/f
JOTO9Q(_/Y.CY0/* Create a local array and load data */
C^ a9J?1\)k0 dynamicData = mxMalloc(ELEMENTS * sizeof(double));
9E6M,e6Bee,s%t,\0 for ( index = 0; index < ELEMENTS; index++ ) {51Testing软件测试网 yus#Hyy N8v*K
dynamicData[index] = data[index];51Testing软件测试网9hadW]7{'Nr-[
}51Testing软件测试网6vk3V%h \V)cn!M'o
c5z;z0w1l*|s |c0/* Create a 2-by-2 mxArray; you will copy existing data into it */
9E o9^7p C_?0 plhs[0] = mxCreateNumericMatrix(ROWS, COLUMNS, mxDOUBLE_CLASS, mxREAL);51Testing软件测试网X`'M;y,V#pa8N3KC
pointer = mxGetPr(plhs[0]);51Testing软件测试网JNF
?qKf}-X
51Testing软件测试网}r]4j!D,W?*|a
/* Copy data into the mxArray */
,qB _3]AR0 for ( index = 0; index < ELEMENTS; index++ ) {
.m{w$gq/MF0 pointer[index] = dynamicData[index];
7j'kQE9N){{AN0 }51Testing软件测试网{|9]Kr
/xb!LhCe0/* You must call mxFree(dynamicData) on the local data.
"n5K'l!K5~+p
Bd0 This is safe because you copied the data into plhs[0] */
dQ&`3w0_A {d0 mxFree(dynamicData);
"w4OkJF zu@G0 51Testing软件测试网&Kl4ow]:hFiGOK8To
return;
(Z,Z4i{(CfR0}51Testing软件测试网1^S"P~+kl