#include "matrix.h"
#include <string>
#include "mex.h"
#define MAX 1000
/* subroutine for filling up data */
void fill( double *pr, mwSize *pm, mwSize *pn, mwSize max )
{
    mwSize 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[] )
{
    mwSize m, n, max=MAX;
    mxArray *rhs[2], *lhs[1];
    mxArray *inplot[2];          /* input to plot function */
//     (void) nlhs; (void) plhs;    /* unused parameters */
//     (void) nrhs; (void) prhs;
      rhs[0]=0;
      mxSetM(rhs[0], 1);
      mxSetN(rhs[0], 1);
      rhs[1] = mxCreateDoubleMatrix(max, 1, mxREAL);
      /* pass the pointers and let fill() fill up data */
      fill(mxGetPr(rhs[1]), &m, &n, MAX);
      mxSetM(rhs[1], m);
      mxSetN(rhs[1], n);
      inplot[0] = mxDuplicateArray(rhs[1]);   
      /* get the sin wave */
      std::string func=mxArrayToString(prhs[0]);
      mexCallMATLAB(1, lhs, 2, rhs, func.c_str());
      inplot[1] = mxDuplicateArray(lhs[0]);
      /* plot(rhs, sin(rhs)) */
      mexCallMATLAB(0, NULL, 2, inplot, "plot");
      /* cleanup allocated memory */
      mxDestroyArray(rhs[0]);
      mxDestroyArray(lhs[0]);
      mxDestroyArray(inplot[0]);
      mxDestroyArray(inplot[1]);
      return;
  }