matlab c shared library: capturing matlab function output with mxArray*/mxArray**

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 testfun.m looks like below --
function c = testfun(a, b)
disp('doing testfun()');
c = a + b ;
disp('done testfun()');
end
now I invoked mcc to make the c-wrapper --
user@pc:/tmp/test$ mcc -B csharedlib:libtestfun testfun.m -v
then I have libtestfun.c, libtestfun.h and libtestfun.so files and I create a c file that calls the testfun() like this --
#include <stdio.h>
#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 ;
}
and I am building the executable with --
user@pc:/tmp/test$ mbuild test.c libtestfun.c -L.libtestfun.so -v
the fact is that the signature for the function reads
LIB_libtestfun_C_API bool MW_CALL_CONV mlfTestfun(int nargout, mxArray** c, mxArray* a, mxArray* b);
If you notice, you can see that the output c is declared as mxArray**, therefore I am using mxArray **c in the test.c file.
But when I run the executable, I only see 0.0 as the value of c, but it supposed to be 9.0 --
user@pc:/tmp/test$ ./test
a = 4.0
b = 5.0
c = 0.0
what is going on?
  1. why is the output always declared as mxArray** in the function signature?
  2. why I can't see the outputs produced by the disp() function in testfun.m ?
any help will be appreciated.

3 Commenti

What happens when you do? :
mxArray *c= NULL;
mlfTestfun(1, &c, a, b);
data = mxGetPr(c); //double *data;
printf("c = %f\n", *data);
Try this. You might need to modify the #includes and lib initialize/terminate functions:
#include <stdio.h>
#include "testfun.h"
int run_main(int argc, char **argv)
{
mxArray *a, *b;
mxArray *c = NULL;
double *x ;
double* data;
a = mxCreateDoubleScalar(4);
x = mxGetPr(a);
printf("a = %.1f\n", x[0]);
b = mxCreateDoubleScalar(5); x = mxGetPr(b);
printf("b = %.1f\n", x[0]);
testfunInitialize();
mlfTestfun(1, &c, a, b);
data = mxGetPr(c); //double *data;
printf("c = %f\n", *data);
testfunTerminate();
mxDestroyArray(a);
mxDestroyArray(b);
mxDestroyArray(c);
mclTerminateApplication();
return 0;
}
int main()
{
mclmcrInitialize();
return mclRunMain((mclMainFcnType)run_main,0,NULL);
}

Accedi per commentare.

Risposte (1)

It seg faults because you de-reference an uninitialized pointer with this line:
*c = mxCreateDoubleMatrix(1, 1, mxREAL);
c is a pointer. You can't de-reference it unless you first assign it a valid value. Maybe you need to do something like this instead:
mxArray *c;
:
c = mxCreateDoubleMatrix(1, 1, mxREAL);
mlfTestfun(1, &c, a, b);
x = mxGetPr(c);

1 Commento

the output variables do not have to be created using mxCreate.. because their memory will be created when the compiled .m file is executed.
Your sample code will leak memory on variable c.

Accedi per commentare.

Categorie

Prodotti

Richiesto:

il 3 Ago 2015

Modificato:

il 4 Ago 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by