How to call C code generated with Matlab Coder from the statisticaL package R

2 views (last 30 days)
Bart
Bart on 26 Jun 2014
Commented: Ryan Livingston on 27 Jun 2014
Dear all,
I am generating C code from Matlab coder. I want to call the generated code from R (a statistical package). My preferred option for doing this would be to use the native R function ".C". This function requires that the variable types are explicitly declared within the .C function call. So the function call (from within R) would look something like this....
.C("pca_uev",x=as.double(x),y=as.double(x),u=as.double(u),e=as.double(e),v=as.double(v))
After generating the C code from Matlab, the entry point of the function looks like this:
void pca_uev(const emxArray_real_T *x, const emxArray_real_T *y, emxArray_real_T *u, double e_data[], int e_size[1], emxArray_real_T *v) { .....remainder of code..... }
Question is how to declare variables correctly to the Matlab generated C code. From within R, I only have the types "double", "single", "integer", etc... available. Matlab seems to define its own data structures somewhere/somehow. In the above problem, x and y (inputs) and u, e, v (outputs) would all be matrices.
I understand that C does not have a native data structure for matrix types. So it makes sense that Matlab generates a type for it. Declaring the matrix x as double from R would presumably pass it as a vector.
Does anyone know how to do the declaration correctly - or can point me to the right reference or documentation on this problem?
I attach the complete Matlab generated C code (for completeness).
Thanks,
Bart Mertens
  1 Comment
Image Analyst
Image Analyst on 26 Jun 2014
Get rid of all that, except for the first paragraph, and just attach your m-file with the paperclip icon.

Sign in to comment.

Accepted Answer

Kaustubha Govind
Kaustubha Govind on 27 Jun 2014
  1 Comment
Ryan Livingston
Ryan Livingston on 27 Jun 2014
To add to this, if I were implementing this, my approach would be to write a C function which is easily callable from R. So, it should have the signature that the R native interface requires. Let's say:
void pca_uev_R_Interface(double*, double*, double*, double*)
I'm not sure how R actually passes the arguments so you may need to adjust the signature to take the data in the format passed by R.
Then, the implementation could follow the pattern:
void pca_uev_R_Interface(double* x, double* y, double* u, double* v)
{
/* Unpack the arguments sent from R to get at double* values */
/* This may require changing the layout of the data if R is */
/* not column major. */
/* Now use the resulting double arrays, x, y, u, v to create */
/* emxArrays the way that Kaustubha showed */
emxArray_real_T *px = emxCreateWrapper_real_T(x, m, n);
/* And so on. Call the generated code here and return the results */
/* back to R */
}
See this answer for more details as well as the link that Kaustubha mentioned.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!