3-4 ������������������ MEX ������

¥H¤U¤¶²Ð¤@­Ó²³æªº C µ{¦¡½X¡A¥¦¥i±N¤@­Ó¿é¤J¯Â¶q¡]¸ê®Æ«¬ºA¬° double¡^­¼¥H 2¡A¦¹µ{¦¡½X¤w¤º«Ø¦b MATLAB ¥úºÐ¤¤¡A¨ä¦ì¸m¬°¡G

{MATLAB root}\extern\examples\refbook\timestwo.c

§@ªÌ¤w¦b­ìµ{¦¡½X¥[¤W¸Ô²Óªº¤¤¤åµù¸Ñ¡A¨Ã§ï¦W¬° scalarx2.c¡A§ó§ï«áªº C µ{¦¡½X¤º®e¦C¥X¦p¤U¡G

­ì©lÀÉ¡]03-À³¥Îµ{¦¡¤¶­±/scalarx2.c¡^¡G¡]¦Ç¦â°Ï°ì«ö¨â¤U§Y¥i«þ¨©¡^
/* ¦¹¨ç¦¡¬° MATLAB ªº MEX ÀɮסA¨ä¿é¤J¬°¤@­Ó¯Â¶q¡A¿é¥X«h¬°¦¹¯Â¶qªº¨â­¿¡C */

#include "mex.h"	/* mex.h ¥]§t mxArray µ²ºcªº©w¸q¡A¥H¤Î¨ä¥L¬ÛÃö¸ê°T */

/* prhs = pointer to the right-hand-side arguments¡A§Y«ü¦V¿é¤JÅܼƦCªº«ü¼Ð */
/* prls = pointer to the  left-hand-side arguments¡A§Y«ü¦V¿é¥XÅܼƦCªº«ü¼Ð */
#define IN  prhs[0]	/* ©w¸q IN  ¬°¦¹¨ç¦¡ªº²Ä¤@­Ó¿é¤JÅܼơA¥H«K«áÄò¨ú¥Î */
#define OUT plhs[0]	/* ©w¸q OUT ¬°¦¹¨ç¦¡ªº²Ä¤@­Ó¿é¥XÅܼơA¥H«K«áÄò¨ú¥Î */

/* ¦¹¨ç¦¡ªº¥\¯à¬°±N x ªº²Ä¹s­Ó¤¸¯À­¼¥H2¡A¦b±Nµ²ªG°e¨ì y ªº²Ä¹s­Ó¤¸¯À¡C */
/* ¦¹¨ç¦¡±N·|³Q mexFunction ©Ò©I¥s¡C */
void timestwo(double y[], double x[]) {
	y[0] = 2.0*x[0];
}

/* ¦¹¨ç¦¡¬°©M MATLAB ·¾³qªº¥D­n¨ç¦¡ */
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[] ) {
	double *x, *y;
	int    no_rows, no_cols;
  
	/* Àˬd¿é¥X©M¿é¤JÅܼƭӼƬO§_³£¬O1¡A¨ä¤¤		  */
	/* nrhs = no. of right-hand-side arguments¡]¿é¤JÅܼƭӼơ^*/
	/* nlhs = no. of  left-hand-side arguments¡]¿é¥XÅܼƭӼơ^*/
	if(nrhs!=1)	/* Àˬd¿é¤JÅܼƭӼƬO§_¬O1 */
		mexErrMsgTxt("One input required.");
	if(nlhs>1)	/* Àˬd¿é¥XÅܼƭӼƬO§_¬O1 */
		mexErrMsgTxt("Too many output arguments");
  
	/* Àˬd¿é¤JÅܼƬO§_¦X®æ */
	no_rows = mxGetM(IN);	/* ¾î¦Cºû«× */
	no_cols = mxGetN(IN);	/* ª½¦æºû«× */
	if(!(no_rows==1 && no_cols==1))	/* Àˬd¿é¤JÅܼƬO§_¬°¯Â¶q */
		mexErrMsgTxt("Input must be a scalar.");
	if(mxIsComplex(IN))		/* Àˬd¿é¤JÅܼƬO§_¬°¹ê¼Æ */
		mexErrMsgTxt("Input must be noncomplex.");
	if(!mxIsDouble(IN))		/* Àˬd¿é¤JÅܼƬO§_¬° double */
		mexErrMsgTxt("Input must be a double.");
  
	/* °t¸m°O¾ÐÅéµ¹¿é¥XÅÜ¼Æ */
	OUT = mxCreateDoubleMatrix(no_rows, no_cols, mxREAL);
  
	/* ¨ú±o¿é¤J©M¿é¥XÅܼƪº«ü¼Ð */
	x = mxGetPr(IN);
	y = mxGetPr(OUT);
  
	/* °õ¦æ¹ê»Úªº¹Bºâ¡G±N¿é¤J¯Â¶q­¼¥H2 */
	timestwo(y, x);
}

¤W­± scalarx2.c µ{¦¡½X§¡¤vªþ¤WÂ×´Iµù¸Ñ¡A¬G¦b¦¹¤£¦A»¡©ú¡C±µµÛ¦b MATLAB ¤¤¡A©I¥s C ½s;¹¹ï scalarx2.c µ{¦¡½X¶i¦æ½sͦp¤U¡G

>> mex scalarx2.c ½sͧ¹«á¡A½T»{¥i°õ¦æÀɬO§_¦s¦b¡A¥i¿é¤J¦p¤U¡G >> which scalarx2 D:\matlabBook\MATLABµ{¦¡³]­p¡G¶i¶¥½g\03-À³¥Îµ{¦¡¤¶­±\scalarx2.mexw64

±µµÛ§Y¥i¶i¦æ¦U¶µ´ú¸Õ¡A¥i¿é¤J¦p¤U¡G

>> scalarx2(8.5) ans = 17 >> scalarx2('String input') ??? Input must be a scalar. >> scalarx2([1 2 3]) ??? Input must be a scalar. ­Y¦P®É¦³ scalarx2.m ¤Î scalarx2.mexw64 ¦s¦b©ó¦P¤@¥Ø¿ý¤U¡AMATLAB ·|¿ï¥Î scalarx2.mexw64 ÀɨӰõ¦æ¡A¦Ó©¿²¤ scalarx2.m¡C¦ý¬O¦pªG¿é¤J¡uhelp scalarx2¡v¡AMATLAB ·|¦C¥X scalarx2.m ªº½u¤W»²§U»¡©ú¡]¦]¬°µLªk¦b scalarx2.c ¤Î scalarx2.mexw64 Â\¸m¬ÛÃöªº½u¤W»²§U»¡©ú¡^¡C´«¥y¸Ü»¡¡A¦³Ãö scalarx2.mexw64 ªº½u¤W»²§U»¡©ú¡A´N¥²¶·¸m©ó scalarx2.m ¤§¤º¡C¨Ò¦p§Ú­Ì¥i¥HÅã¥Ü scalarx2.m ªº¤º®e¡G >> type scalarx2.m function out = scalarx2(in) % SCALARX2 A scalar version of "times two". % This serves as an example of putting on-line help in an % M-file, but the actual program body is in another MEX-file % with the same major file name. ©ÎÅã¥Ü scalarx2.m ªº»¡©ú¡G >> help scalarx2 SCALARX2 A scalar version of "times two". This serves as an example of putting on-line help in an M-file, but the actual program body is in another MEX-file with the same major file name. ¦ý­Y­n¶i¦æ°õ¦æ®É¡A§Ú­Ì¥i¥H¨Ï¥Î which «ü¥O¨ÓÅã¥Ü scalarx2 ©Ò¹ïÀ³ªº°õ¦æÀɮ׬O MEX ÀɮצӤ£¬O M ÀɮסG >> which scalarx2 D:\matlabBook\MATLABµ{¦¡³]­p¡G¶i¶¥½g\03-À³¥Îµ{¦¡¤¶­±\scalarx2.mexw64
MATLABµ{¦¡³]­p¡G¶i¶¥½g