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
  1 件のコメント
Image Analyst
Image Analyst 2014 年 6 月 26 日
Get rid of all that, except for the first paragraph, and just attach your m-file with the paperclip icon.

サインインしてコメントする。

採用された回答

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 Center および File ExchangeMATLAB Coder についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by