title: How to Convert a structure from Matlab code to C code (using Matlab Compiler)
body: <p>I have been trying to create a C Shared Library using Matlab compiler that will be used as a plug in library to a different application for a while now. I recently thought I had completed this task only to realize that the function I was calling from my newly "Matlab Compiled" Shared Library needed to convert its return to a C structure. </p>
  
  <p>I used the example found on the Matlab Answers site to help me create the wrapper level2 function to call my Matlab function which needs to return a structure.(<a href="http://www.mathworks.com/matlabcentral/answers/94715-how-do-i-wrap-matlab-compiler-4-8-r2008a-created-c-dlls-to-create-another-dll" rel="nofollow">http://www.mathworks.com/matlabcentral/answers/94715-how-do-i-wrap-matlab-compiler-4-8-r2008a-created-c-dlls-to-create-another-dll</a>)  </p>
  
  <p>My issue is at the <em>Convert returned  MATLAB data to C data</em> part of the code found below. I can convert to ints, doubles, chars, etc. fine, but I am having trouble figuring out how to code the conversion from an mxArray returned by matlab to a C structure.</p>
  
  <pre><code>/* Wrapper for level 1 function exported by the MATLAB generated DLL         *
   * This function converts C data to MATLAB data, calls the MATLAB generated  *
   * function in level1.dll and then converts the MATLAB data back into C data */
  
  int wmlfLevel1(double* input2D, int size, char* message, double** output2d){
      int nargout=1;
  
      /* Pointers to MATLAB data */
      mxArray *msg;
      mxArray *in2d;
      mxArray *out2d=NULL;
  
      /* Start MCR, load library if not done already */
      int returnval=isMCRrunning();
      if(!returnval)
          return returnval;
  
      /* Convert C data to MATLAB data */
      /* IMPORTANT: this has to be done after ensuring that the MCR is running */
      msg=mxCreateString(message);
      in2d=mxCreateDoubleMatrix(size, size, mxREAL);
      memcpy(mxGetPr(in2d), input2D, size*size*sizeof(double));
  
      /* Call the M function */
      returnval=mlfLevel1(nargout, &amp;out2d, in2d, msg);
  
      /*Convert returned  MATLAB data to C data */
      *output2d=(double *)malloc(sizeof(double)*size*size);
      memcpy(*output2d, mxGetPr(out2d), size*size*sizeof(double));
  
      /* Clean up MATLAB variables */
      mxDestroyArray(msg);
      mxDestroyArray(in2d);
      mxDestroyArray(out2d);
  
      return returnval;
  }
  </code></pre>
  
  <p>So far I have tried using the mxCreateStructMatrix function, I tried creating a C structure skeleton, i am about to try the libstruct function, but as I am new to C Programming and the Matlab compiler any assistance would be greatly appreciated!</p>