title: How Do I use Mexcallmatlab to Call a User-defined function?
body: <p>I am trying to parallelize a section of my Matlab code using OpenMP in a mex file. The section in theMatlab code that I want to  parallelize is:</p>
  
  <p>for i = 1 : n</p>
  
  <pre><code>    D(:, i) = CALC(A, B(:,i), C(i));
  
  end
  </code></pre>
  
  <p>I have written this in order to parallelize it:</p>
  
  <pre><code> void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
  {
  size_t r,n,i,G;
  double *A, *B, *C, *D;
  int nthreads;
  nthreads = 4;
  A = mxGetPr(prhs[0]); /* first input matrix */
  B = mxGetPr(prhs[1]); /* second input matrix */
  C = mxGetPr(prhs[2]);/* third input matrix */
  /* dimensions of input matrices */
  r = mxGetN(prhs[0]);
  n = mxGetN(prhs[1]);
  plhs[0] = mxCreateDoubleMatrix(r,n, mxREAL);
  D = mxGetPr(plhs[0]);
  G=n/nthreads;
  omp_set_num_threads(nthreads);
  #pragma omp parallel for schedule (dynamic, G)
    {
     for i = 1 : n
          D(:, i) = CALC(A, B(:,i), C(i));
    }
   }
  </code></pre>
  
  <p>CALC is a Matlab function I have written. My challenge is how to use Mexcallmatlab to call in the CALC function to the mex file so that it can execute it in parallel inside my mex file, and return the elements of each column of D (i.e. D(:, i) back to my Matlab code. </p>
  
  <p>Sorry for the lenghty question. Any help I can get on this will be highly appreciated.</p>