title: matlab c shared library: capturing matlab function output with mxArray*/mxArray**
body: <p>I am trying to call matlab function from c code, trying to follow whatever I can get over the web. I am using matlab version R2014a running on Ubuntu 14.04. Lets say the function <code>testfun.m</code> looks like below --</p>
  
  <pre><code>function c = testfun(a, b)
      disp('doing testfun()');
      c = a + b ;
      disp('done testfun()');
  end
  </code></pre>
  
  <p>now I invoked <code>mcc</code> to make the c-wrapper --</p>
  
  <pre><code>user@pc:/tmp/test$ mcc -B csharedlib:libtestfun testfun.m -v
  </code></pre>
  
  <p>then I have <a href="http://pastebin.com/T46vHTm4" rel="nofollow"><code>libtestfun.c</code></a>, <a href="http://pastebin.com/TSEjJtHr" rel="nofollow"><code>libtestfun.h</code></a> and <code>libtestfun.so</code> files and I create a c file that calls the <code>testfun()</code> like this --</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include "libtestfun.h"
  
  int main()
  {
      libtestfunInitialize();
  
      mxArray *a, *b, **c;
      double *x ;
  
      a = mxCreateDoubleScalar(4); x = mxGetPr(a);
      printf("a = %.1f\n", x[0]);
      b = mxCreateDoubleScalar(5); x = mxGetPr(b);
      printf("b = %.1f\n", x[0]);
  
      *c = mxCreateDoubleMatrix(1, 1, mxREAL);
      mlfTestfun(1, c, a, b);
  
      x = mxGetPr(c[0]);
      printf("c = %.1f\n", x[0]);
  
      libtestfunTerminate();
      return 1 ;
  }
  </code></pre>
  
  <p>and I am building the executable with --</p>
  
  <pre><code>user@pc:/tmp/test$ mbuild test.c libtestfun.c -L.libtestfun.so -v
  </code></pre>
  
  <p>the fact is that the signature for the function reads </p>
  
  <pre><code>LIB_libtestfun_C_API bool MW_CALL_CONV mlfTestfun(int nargout, mxArray** c, mxArray* a, mxArray* b);
  </code></pre>
  
  <p>If you notice, you can see that the output <code>c</code> is declared as <code>mxArray**</code>, therefore I am using <code>mxArray **c</code> in the <code>test.c</code> file.</p>
  
  <p>But when I run the executable, I only see <code>0.0</code> as the value of <code>c</code>, but it supposed to be <code>9.0</code> --</p>
  
  <pre><code>user@pc:/tmp/test$ ./test 
  a = 4.0
  b = 5.0
  c = 0.0
  </code></pre>
  
  <p>what is going on? </p>
  
  <p>why is the output always declared as <code>mxArray**</code> in the function signature?
  why I can't see the outputs produced by the <code>disp()</code> function in <code>testfun.m</code> ? </p>
  
  <p>any help will be appreciated.</p>