I have the function in Matlab
function y = foo(x) y = 2*x; end
where x is an array with a dynamic size. I have generated code/dll for that function using the Matlab Coder toolbox. Since x and y have dynamic size, x and y are pointers to the emxArray structure in the generated code and both are passed to the generated functions as arguments. In order not to have the emxArray structure at the interface to the generated code, I created a wrapper function in C, that looks like that
double* wrapper_foo(const double *x, const int nrOfElements){ foo_initialize(); double* y = NULL; y = (double*) malloc(nrOfElements * sizeof(double)); emxArray_real_T* pEmxX; emxArray_real_T* pEmxY; pEmxX = emxCreateWrapper_real_T(x, 1, nrOfElements); pEmxY = emxCreateWrapper_real_T(y, 1, nrOfElements); foo(pEmxX, pEmxY); double* ptr1 = y; double* ptr2 = pEmxY->data; for (size_t i = 0; i < nrOfElements; i++, ptr1++, ptr2++) { *ptr1 = *ptr2; } ptr1 = NULL; ptr2 = NULL; emxDestroyArray_real_T(pEmxX); emxDestroyArray_real_T(pEmxY); foo_terminate(); return y;}
I can now call wrapper function now from my main function instead of calling the foo function. Now, what I want to do more is to create the dll of this function and add it to the foo library that I generated before, i.e. link the wrapper_foo to foo.lib that was the product after code generation with the Matlab Coder. Is there a function in the Matlab Coder Toolbox or generally in Matlab to do this?
Best Answer