Download 7 - Parent Directory

Transcript
Examples of C MEX-Files
#include "mex.h"
/* Computational subroutine */
void convec(double *xr, double *xi, int nx, double *yr,
double *yi, int ny, double *zr, double *zi)
{
int i,j;
zr[0] = 0.0;
zi[0] = 0.0;
/* Perform the convolution of the complex vectors. */
for (i = 0; i < nx; i++) {
for (j = 0; j < ny; j++) {
*(zr+i+j) = *(zr+i+j) + *(xr+i) * *(yr+j) - *(xi+i)
* *(yi+j);
*(zi+i+j) = *(zi+i+j) + *(xr+i) * *(yi+j) + *(xi+i)
* *(yr+j);
}
}
}
Below is the gateway routine that calls this complex convolution.
/* The gateway routine. */
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
double *xr, *xi, *yr, *yi, *zr, *zi;
int
rows, cols, nx, ny;
/* Check for the proper number of arguments. */
if (nrhs != 2)
mexErrMsgTxt("Two inputs required.");
if (nlhs > 1)
mexErrMsgTxt("Too many output arguments.");
/* Check that both inputs are row vectors. */
if (mxGetM(prhs[0]) != 1 || mxGetM(prhs[1]) != 1)
mexErrMsgTxt("Both inputs must be row vectors.");
rows = 1;
4-21