User`s manual

2 Creating C Language MEX-Files
2-34
* This is a MEX-file for MATLAB.
* Copyright (c) 1984-2000 The MathWorks, Inc.
*============================================================*/
#include "mex.h"
#define MAX 1000
/* Subroutine for filling up data */
void fill( double *pr, int *pm, int *pn, int max )
{
int i;
/* You can fill up to max elements, so (*pr) <= max. */
*pm = max/2;
*pn = 1;
for(i = 0; i < (*pm); i++)
pr[i] = i*(4*3.14159/max);
}
/* Gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] )
{
int m, n, max = MAX;
mxArray *rhs[1], *lhs[1];
rhs[0] = mxCreateDoubleMatrix(max, 1, mxREAL);
/* Pass the pointers and let fill() fill up data. */
fill(mxGetPr(rhs[0]), &m, &n, MAX);
mxSetM(rhs[0], m);
mxSetN(rhs[0], n);
/* Get the sin wave and plot it. */
mexCallMATLAB(1, lhs, 1, rhs, "sin");
mexCallMATLAB(0, NULL, 1, lhs, "plot");
/* Clean up allocated memory. */
mxDestroyArray(rhs[0]);