如何在带有 emxArray 参数的 C 程序中使用由 MATLAB Coder codegen 创建的 C 库? [英] How to use C library created by MATLAB Coder codegen in C program with emxArray arguments?

查看:22
本文介绍了如何在带有 emxArray 参数的 C 程序中使用由 MATLAB Coder codegen 创建的 C 库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由 codegen 创建的 C 函数(C 静态库)采用 const emxArray_uint32_T 类型的输入参数并返回 emxArray_struct_T 类型的值.正如类型所暗示的,输入是一个 uint32 数组,输出是一个结构数组.

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.

我不确定如何在我的 C 程序中使用这个函数.对于输入,我应该声明一个 uint32_T 类型的数组还是使用 emxArray_uint32_T 类型的数组?对于输出,因为不知道输出数组的大小,如何声明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?

我把问题放在 MATLAB 答案中,但运气不佳..

I put the question in MATLAB answers but have not luck..

谢谢!

推荐答案

如果您使用过 C++,emxArray 数据类型就像生成的 C 等效的 std::vector.也就是说,这就是生成的代码如何表示动态分配的数组.它们存储数据和大小指针以及其他一些细节.

If you've used C++, the emxArray data types are like generated C equivalents of std::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.

如果您查看生成代码的目录,您应该会找到一个名为 _emxAPI.h 的文件.该文件声明了一些实用函数,它们使构造和销毁 emxArray 值变得更简单.使用它们来创建 emxArray 值可确保所有字段都正确初始化,并使您的代码免受对 emxArray 类型的任何可能更改的影响.

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.

在我制作的一个例子中,它接受一个 uint32 值数组并返回这样一个数组,我看到以下函数:

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);

前四个函数可用于在不同情况下创建emxArray值.

The first four functions can be used to create emxArray values in different situations.

第一对,即emxCreateWrapper_uint32_T, emxCreateWrapperND_uint32_T,可用于创建一个uint32 emxArray,从现有数据.因此,如果您已经在某个内存中分配了输入数据,这些函数会将这些数据包装到一个指定大小的 emxArray 中,而无需为您的数据分配额外的内存.

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 extra 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);

第二对,即 emxCreate_uint32_T、emxCreateND_uint32_T,也创建 emxArray 值.然而,它们也为 emxArraydata 字段堆分配存储空间.此内存将足够大以容纳在其各自大小参数中指定的元素数量调用这些后,您将需要填充存储在返回的 emxArraydata 字段中的数据代码>结构:

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);

最后一个 emxDestroyArray_uint32_T 将用于销毁数组并释放之前方法分配的所有内存.

The last, emxDestroyArray_uint32_T, will be used to destroy the array and deallocate any memory allocated by the previous methods.

最后,为了捕获您的输出,您可以使用 emxCreate_struct_TemxCreateND_struct_T 创建一个空的 emxArray struct_T> 通过在适当的情况下为一种或多种尺寸传递 0 来获得具有适当维数的值.生成的代码将分配足够的内存以在运行时将结果数据保存在输出 emxArray 中.然后,您可以检查此输出 emxArraysize 字段以查看 data 字段的维度大小并根据需要提取数据.

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.

有关使用 emxArray 参数的文档可用 此处.

The documentation for using emxArray arguments is available here.

这篇关于如何在带有 emxArray 参数的 C 程序中使用由 MATLAB Coder codegen 创建的 C 库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆