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 an array of struct.
I'm not sure how to 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 don't know the size of the output array, how to declare the array of struct to receive the return values from the function?
I put the question in MATLAB answers but have not luck..
Thanks!
You need to use emxArray_uint32_T and emxArray_struct_T. All of the MATLAB Coder defined datatypes that the code uses (and you need to use) are defined in the YourLibName_types.h header file.
If you've used C++, the
emxArray
data types are like generated C equivalents ofstd::vector
. Namely, this is how the generated code represents dynamically allocated arrays. They store data and size pointers as well as a few other details.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 destroyingemxArray
values simpler. Using them to createemxArray
values ensures that all of the fields are properly initialized and insulates your code from any possible changes to theemxArray
type.In an example I made which takes an array of
uint32
values and also returns such an array, I see the following functions: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 auint32
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 anemxArray
of the specified size without allocating extra memory for your data.The second pair, i.e.
emxCreate_uint32_T, emxCreateND_uint32_T
, also createemxArray
values. However, they also heap allocate storage for thedata
field of theemxArray
. 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 thedata
field of the returnedemxArray
struct: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
oremxCreateND_struct_T
to create an emptyemxArray
ofstruct_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 outputemxArray
at runtime. You can then check thesize
field of this outputemxArray
to view the sizes of the dimensions of thedata
field and extract the data as you wish.The documentation for using
emxArray
arguments is available here.