Question about using C library created by codegen in C program

조회 수: 2 (최근 30일)
Jeff
Jeff 2014년 6월 14일
댓글: Kara 2014년 10월 3일
The C function (C static library) created by codegen takes an input argument of type const emxArray_uint32_T and return values of type emxArray_struct_T. As the type suggests, input is an array of uint32 and output is array of struct.
I'm not sure how I should use this function in my C program. For the input, should I declare an array of type uint32_T or use the type emxArray_uint32_T ? For the output, because I won't know the size of the output array, how should I declare the array of struct to receive the return values from the function?
Thanks!

채택된 답변

Ryan Livingston
Ryan Livingston 2014년 6월 17일
편집: Ryan Livingston 2014년 6월 17일
Hi Jeff,
I added the product MATLAB Coder to your question since this makes searching easier.
The following is a duplication of my answer at StackOverflow:
If you look in the directory where you generated code you should find a file named functionName_emxAPI.h. This file declares some utility functions which make constructing and destroying emxArray values simpler. Using them to create emxArray values ensures that all of the fields are properly initialized and insulates your code from any possible changes to the emxArray type.
In an example I made which takes an array of uint32 values and also returns such an array I see the following functions:
extern emxArray_uint32_T *emxCreateWrapperND_uint32_T(unsigned int *data,
int numDimensions,
int *size);
extern emxArray_uint32_T *emxCreateWrapper_uint32_T(unsigned int *data, int rows,
int cols);
extern emxArray_uint32_T *emxCreateND_uint32_T(int numDimensions, int *size);
extern emxArray_uint32_T *emxCreate_uint32_T(int rows, int cols);
extern void emxDestroyArray_uint32_T(emxArray_uint32_T *emxArray);
The first four functions can be used to create emxArray values in different situations.
The first pair, i.e. emxCreateWrapper_uint32_T, emxCreateWrapperND_uint32_T, can be used to create a uint32 emxArray with the specified number of dimensions and sizes from existing data. So if you already have the input data allocated in some memory, these functions wrap that data up into an emxArray of the specified size without allocating any extra memory.
/* Create a 10-by-10 C array of uint32 values and wrap an emxArray around it */
uint32_T x[100];
emxArray *pEmx = NULL;
int k = 0;
for (k = 0; k < 100; k++) {
x[k] = (uint32_T) k;
}
pEmx = emxCreateWrapper_uint32_T(x, 10, 10);
/* Use pEmx here*/
/* Deallocate any memory allocated in pEmx. */
/* This DOES NOT free pEmx->data because the "wrapper" function was used */
emxDestroyArray_uint32_T(pEmx);
The second pair, i.e. emxCreate_uint32_T, emxCreateND_uint32_T, also create emxArray values. However, they also heap allocate storage for the data field of the emxArray. This memory will be large enough to hold the number of elements specified in their respective size arguments After calling these, you will need to populate the data stored in the data field of the returned emxArray struct:
/* Allocate a 10-by-10 uint32 emxArray and fill the values */
int k = 0;
emxArray *pEmx = emxCreate_uint32_T(10, 10);
for (k = 0; k < 100; ++k) {
pEmx->data[k] = (uint32_T) k;
}
/* Use pEmx here*/
/* Deallocate any memory allocated in pEmx. */
/* This DOES free pEmx->data */
emxDestroyArray_uint32_T(pEmx);
The last, emxDestroyArray_uint32_T, will be used to destroy the array and deallocate any memory allocated by the previous methods.
Finally, to capture your output, you could use emxCreate_struct_T or emxCreateND_struct_T to create an empty emxArray of struct_T values with the proper number of dimensions by passing 0 for one or more sizes where appropriate. The generated code will allocate enough memory to hold the resulting data in your output emxArray at runtime. You can then check the size field of this output emxArray to view the sizes of the dimensions of the data field and extract the data as you wish.
The documentation for using emxArray arguments is available here:
  댓글 수: 2
Jeff
Jeff 2014년 6월 17일
편집: Jeff 2014년 6월 17일
Thank you so much Ryan for your detailed and clear explanation! That really helps!
Kara
Kara 2014년 10월 3일
I'm having trouble with the emxArray structure as well. I tried implementing the example above for my data type, but when I try to build my solution, I get the following error:
Error 1 error LNK2019: unresolved external symbol "struct emxArray_uint16_T * __cdecl emxCreateWrapper_uint16_T(unsigned short *,int,int)" (?emxCreateWrapper_uint16_T@@YAPAUemxArray_uint16_T@@PAGHH@Z) referenced in function _wmain
Any suggestions?
Thanks!!

댓글을 달려면 로그인하십시오.

추가 답변 (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