【问题标题】:how to set a vector element in a mex structure如何在 mex 结构中设置向量元素
【发布时间】:2012-05-23 06:26:02
【问题描述】:

我正在尝试在 mex 函数中填充结构。我有在 mexFunction 中创建的结构的基础知识,但是当结构中的一个字段是一个数组并且我想在“for”循环迭代时填充这个数组中的每个元素时,它会变得有点混乱。

....
mxArray *value;
mwSize dims[2] = {16,8};
int numFields = 2;  
const char *fieldNames[] = {"array1", "array2"}; 
plhs[2] = mxCreateStructArray(2, dims, numFields, fieldNames);

for (int i = 0; i < 16; i++)
{
    for (int j = 0; j < 8; j ++)
    {
        value1 = (some calculation);
        value = mxCreateDoubleMatrix(1,18,mxREAL); 
        *mxGetPr(value[sampleIndex]) = value1;
        mxSetField(plhs[2], i, "array1", value);
        // i want to set the array1[j] element

        value2 = (some other calculation);
        value = mxCreateDoubleMatrix(1,8,mxREAL); 
        *mxGetPr(value) = value2;
        mxSetField(plhs[2], i, "array2", value);


    }
}
....

字段“array1”和“array2”都是相同大小的向量(比如说 8 个元素)我不知道如何填充每个单独的元素。如何通过数组索引'j'设置字段名称。

为了便于解释,结构应该如下所示: 结构体是一个16个元素的数组,每个元素有2个字段,每个字段有8个元素

structure(0).array1 = [1 2 3 4 5 6 7 8];
structure(0).array2 = [11 12 13 14 15 16 17 18];

structure(1).array1 = [21 22 23 24 25 26 27 28];
structure(1).array2 = [211 212 213 214 215 216 217 218];

structure(2).array1 = [31 32 33 34 35 36 37 38];
structure(2).array2 = [311 312 313 314 315 316 317 318];

structure(3).array1 = [41 42 43 44 45 46 47 48];
structure(3).array2 = [411 412 413 414 415 416 417 418];
...

这些值是任意的。

【问题讨论】:

    标签: c arrays matlab structure mex


    【解决方案1】:

    考虑以下 C 代码:

    structArrayMEX.c

    #include "mex.h"
    
    void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
    {   
        mxArray *x, *y;
        double *xData, *yData;
        mwIndex idx, i;
        int counter = 1;
    
        // create a 16x1 structure array, each with two fields x and y
        const char *fieldNames[] = {"x", "y"}; 
        mxArray *s = mxCreateStructMatrix(16, 1, 2, fieldNames);
    
        // fill structure array
        for (idx=0; idx<16; idx++) {
            // create x and y matrices each of size 1x8
            x = mxCreateDoubleMatrix(1, 8, mxREAL);
            y = mxCreateDoubleMatrix(1, 8, mxREAL);
    
            // fill x and y matrices: x[i], y[i]
            xData = mxGetPr(x);
            yData = mxGetPr(y);
            for (i=0; i<8; i++) {
                xData[i] = counter;
                yData[i] = counter + 100;
                counter++;
            }
    
            // set fields: s(idx).x = x and s(idx).y = y
            mxSetField(s, idx, "x", x);
            mxSetField(s, idx, "y", y);
        }
    
        // return structure array
        plhs[0] = s;
    }
    

    返回的结构体数组:

    >> s = structArrayMEX();
    >> whos s
      Name       Size            Bytes  Class     Attributes
    
      s         16x1              4096  struct         
    
    >> s(1)
    ans = 
        x: [1 2 3 4 5 6 7 8]
        y: [101 102 103 104 105 106 107 108]
    >> s(16)
    ans = 
        x: [121 122 123 124 125 126 127 128]
        y: [221 222 223 224 225 226 227 228]
    

    上面的 MEX 函数应该等价于下面的 MATLAB 代码

    vals = reshape(1:16*8, [8 16])';
    s = struct('x',num2cell(vals,2), 'y',num2cell(vals+100,2));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-20
      • 1970-01-01
      • 2015-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多