How to use the C library created by MATLAB Coder codegen in a C program with emxArray arguments?

The C function (C static library) created by codegen takes an input argument of type const emxArray_uint32_T and returns values โ€‹โ€‹of type emxArray_struct_T . As the type suggests, input is an uint32 array, and output is an array of structures.

I am not sure how to use this function in my C program. To enter I have to declare an array of type uint32_T or use type emxArray_uint32_T ? For output, because I do not know the size of the output array, how to declare an array of structure to get return values โ€‹โ€‹from a function?

I posed a question in MATLAB answers, but no luck ..

Thanks!

+2
c types matlab matlab-coder codegen
source share
2 answers

If you look in the directory where you generated the code, you should find a file named <functionName>_emxAPI.h . This file declares some utility functions that simplify the construction and destruction of emxArray values. Using them to create emxArray values โ€‹โ€‹ensures that all fields are properly initialized and isolates your code from any possible changes to the emxArray type.

In the example I did, 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. Therefore, if you already have input data allocated in some memory, these functions transfer this data to emxArray specified size without allocating additional memory for your data.

 /* 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 creates emxArray values. However, they also provide a bunch of storage for the data emxArray field. This memory will be large enough to hold the number of elements specified in their respective size arguments. After calling them, you will need to fill in the data stored in the data field of the returned emxArray structure:

 /* 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 latter, emxDestroyArray_uint32_T , will be used to destroy the array and free memory allocated by previous methods.

Finally, to record your output, you can use emxCreate_struct_T or emxCreateND_struct_T to create empty emxArray values โ€‹โ€‹with the appropriate number of dimensions, passing 0 for one or more sizes where necessary. The generated code will allocate enough memory to store the resulting data on your emxArray output at runtime. You can then check the size field of this emxArray output to see the sizes of the data field and retrieve the data as you like.

The documentation for using emxArray arguments is available here .

+4
source share

You need to use emxArray_uint32_T and emxArray_struct_T . All MATLAB Coder defined data types that the code uses (and you need to use) are defined in the YourLibName_types.h header file.

+1
source share

All Articles