3-4 一??簡單??MEX 檔?

HUФ@²檺 C {XAiN@ӿJ¶q]ƫA double^H 2A{Xwئb MATLAB ФAmG

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

@̤wb{X[WԲӪѡAçW scalarx2.cA᪺ C {XeCXpUG

lɡ]03-ε{/scalarx2.c^G]ǦϰUYi^
/* 禡 MATLAB  MEX ɮסAJ@ӯ¶qAXh¶q⭿C */

#include "mex.h"	/* mex.h ]t mxArray cwqAHΨLT */

/* prhs = pointer to the right-hand-side argumentsAYVJܼƦC */
/* prls = pointer to the  left-hand-side argumentsAYVXܼƦC */
#define IN  prhs[0]	/* wq IN  禡Ĥ@ӿJܼơAHK */
#define OUT plhs[0]	/* wq OUT 禡Ĥ@ӿXܼơAHK */

/* 禡\ରN x ĹsӤH2AbNGe y ĹsӤC */
/* 禡N|Q mexFunction ҩIsC */
void timestwo(double y[], double x[]) {
	y[0] = 2.0*x[0];
}

/* 禡M MATLAB qDn禡 */
void mexFunction( int nlhs, mxArray *plhs[],
                  int nrhs, const mxArray *prhs[] ) {
	double *x, *y;
	int    no_rows, no_cols;
  
	/* ˬdXMJܼƭӼƬO_O1A䤤		  */
	/* nrhs = no. of right-hand-side arguments]JܼƭӼơ^*/
	/* nlhs = no. of  left-hand-side arguments]XܼƭӼơ^*/
	if(nrhs!=1)	/* ˬdJܼƭӼƬO_O1 */
		mexErrMsgTxt("One input required.");
	if(nlhs>1)	/* ˬdXܼƭӼƬO_O1 */
		mexErrMsgTxt("Too many output arguments");
  
	/* ˬdJܼƬO_X */
	no_rows = mxGetM(IN);	/* C */
	no_cols = mxGetN(IN);	/*  */
	if(!(no_rows==1 && no_cols==1))	/* ˬdJܼƬO_¶q */
		mexErrMsgTxt("Input must be a scalar.");
	if(mxIsComplex(IN))		/* ˬdJܼƬO_ */
		mexErrMsgTxt("Input must be noncomplex.");
	if(!mxIsDouble(IN))		/* ˬdJܼƬO_ double */
		mexErrMsgTxt("Input must be a double.");
  
	/* tmO鵹Xܼ */
	OUT = mxCreateDoubleMatrix(no_rows, no_cols, mxREAL);
  
	/* oJMXܼƪ */
	x = mxGetPr(IN);
	y = mxGetPr(OUT);
  
	/* ڪBGNJ¶qH2 */
	timestwo(y, x);
}

W scalarx2.c {XvW״IѡAGbACۦb MATLAB AIs C sĶ scalarx2.c {XisĶpUG

>> mex scalarx2.c sĶAT{iɬO_sbAiJpUG >> which scalarx2 D:\matlabBook\MATLAB{]pGig\03-ε{\scalarx2.mexw64

ۧYiiUաAiJpUG

>> scalarx2(8.5) ans = 17 >> scalarx2('String input') ??? Input must be a scalar. >> scalarx2([1 2 3]) ??? Input must be a scalar. YPɦ scalarx2.m scalarx2.mexw64 sbP@ؿUAMATLAB | scalarx2.mexw64 ɨӰAө scalarx2.mCOpGJuhelp scalarx2vAMATLAB |CX scalarx2.m uWU]]Lkb scalarx2.c scalarx2.mexw64 \muWU^CyܻA scalarx2.mexw64 uWUANm scalarx2.m CҦpڭ̥iH scalarx2.m eG >> 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. YniɡAڭ̥iHϥ which O scalarx2 ҹɮ׬O MEX ɮצӤO M ɮסG >> which scalarx2 D:\matlabBook\MATLAB{]pGig\03-ε{\scalarx2.mexw64
MATLAB{]pGig