User`s manual
Examples of C MEX-Files
2-15
void xtimesy(double x, double *y, double *z, int m, int n)
{
int i,j,count = 0;
for(i = 0; i < n; i++) {
for(j = 0; j < m; j++) {
*(z+count) = x * *(y+count);
count++;
}
}
}
/* The gateway routine */
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
double *y,*z;
double x;
int status,mrows,ncols;
/* Check for proper number of arguments. */
/* NOTE: You do not need an else statement when using
mexErrMsgTxt within an if statement. It will never
get to the else statement if mexErrMsgTxt is executed.
(mexErrMsgTxt breaks you out of the MEX-file.)
*/
if(nrhs != 2)
mexErrMsgTxt("Two inputs required.");
if(nlhs != 1)
mexErrMsgTxt("One output required.");
/* Check to make sure the first input argument is a scalar. */
if(!mxIsDouble(prhs[0]) || mxIsComplex(prhs[0]) ||
mxGetN(prhs[0])*mxGetM(prhs[0]) != 1 ) {
mexErrMsgTxt("Input x must be a scalar.");
}
/* Get the scalar input x. */
x = mxGetScalar(prhs[0]);