【发布时间】:2014-08-29 17:16:15
【问题描述】:
我正在尝试调用一些由 Matlab 编码器生成的 c 代码。 Matlab 使用一个名为 emxArray 的 c 结构来表示矩阵(在此处记录:http://www.mathworks.co.uk/help/fixedpoint/ug/c-code-interface-for-unbounded-arrays-and-structure-fields.html)。
struct emxArray_real_T
{
double *data;
int *size;
int allocatedSize;
int numDimensions;
boolean_T canFreeData;
};
我几乎没有 ctypes 的经验,并且正在努力创建一个等效的结构,然后我可以使用它来来回传递向量到 c .so 中定义的函数
这是我迄今为止在 python 中所取得的成就......
class EmxArray(ctypes.Structure):
""" creates a struct to match emxArray_real_T """
_fields_ = [('data', ctypes.POINTER(ctypes.c_double)),
('size', ctypes.POINTER(ctypes.c_int)),
('allocatedSize', ctypes.c_int),
('numDimensions', ctypes.c_int),
('canFreeData', ctypes.c_bool)]
但是,如果我这样定义:
data = (1.1, 1.2, 1.3, 1.4)
L = len(data)
x = EmxArray()
x.data = (ctypes.c_double * L)(*data)
x.data = (ctypes.c_int * 1)(L)
这样就可以了
print len(x.data[:L])
for v in x.data[:L]: print v
编辑:我已经整理并采纳了Roland的建议,可以使用
data_out = x.data[:L]
我需要进一步调查,看看我是否可以成功地使用这个结构来传递和接收来自 c 代码的数据。
解决方案
按照 Roland 的建议实现 ctypes 结构不起作用 - 返回的值是垃圾,当我追求基于 python 的 lilbil 答案的实现时,我从来没有弄清楚为什么。我已经接受了这个答案,因为它是最接近的......
我将在这里记录我的解决方案,因为它可能会节省其他人像我一样浪费时间。
首先,我生成了一个简单的 matlab 函数,它将函数的每个元素自身相乘,并使用编码器将其编译为 c .so。这是使用 ctypes 导入到 python 的。代码如下...
import ctypes
LIBTEST = '..../dll/emx_test/'
EMX = ctypes.cdll.LoadLibrary(LIBTEST + 'emx_test.so')
init = EMX.emx_test_initialize()
# Create a data structure to hold the pointer generated by emxCreateWrapper...
class Opaque(ctypes.Structure):
pass
# make some random data to pass in
data_in = [1., 2., 4., 8., 16.]
L = len(data_in)
# create an empty array of the same size for the output
data_ou = [0] * L
# put this in a ctypes array
ina = (ctypes.c_double * L)(*data_in)
oua = (ctypes.c_double * L)(*data_ou)
# create a pointer for these arrays & set the rows and columns of the matrix
inp = ctypes.pointer(ina)
oup = ctypes.pointer(oua)
nrows = ctypes.c_int(1)
ncols = ctypes.c_int(L)
# use EMX.emxCreateWrapper_real_T(double *data, int rows, int cols) to generate an emx wrapping the data
# input arg types are a pointer to the data NOTE its not great to have to resize the ctypes.c_double but cant see another way
EMX.emxCreateWrapper_real_T.argtypes = [ctypes.POINTER(ctypes.c_double * L), ctypes.c_int, ctypes.c_int]
# a pointer to the emxArray is returned and stored in Opaque
EMX.emxCreateWrapper_real_T.restype = ctypes.POINTER(Opaque)
# use emxCreateWrapper
in_emx = EMX.emxCreateWrapper_real_T(inp, nrows, ncols)
ou_emx = EMX.emxCreateWrapper_real_T(oup, nrows, ncols)
# so now we have to emx's created and have pointers to them we can run the emx_test
# emx test looks like this in matlab
#
# function res = emx_test ( in )
# res = in .* in;
# end
#
# so basically it multiplies each element of the matrix by itself
#
# therefore [1., 2., 4., 8., 16.] should become [1., 4., 8., 64., 256.]
EMX.emx_test(in_emx, ou_emx)
# and voila...that's what we get
print 'In: ', ina[:L]
print 'Out:', oua[:L]
输出:
In: [1.0, 2.0, 4.0, 8.0, 16.0]
Out:[1.0, 4.0, 16.0, 64.0, 256.0]
感谢大家的宝贵时间和建议。
【问题讨论】:
-
我觉得你可能不需要
sz和...*sz))' parts of theEmxArray` 类。 -
我这样做的原因是数据将是一个数组,但是如果我可以允许一个可变大小的数组,那将是最好的。
标签: python ctypes matlab-coder