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

3 次查看(过去 30 天)
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

采纳的回答

Kaustubha Govind
Kaustubha Govind 2014-6-27
  1 个评论
Ryan Livingston
Ryan Livingston 2014-6-27
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB Coder 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by