I’m trying to populate a structure in a mex function. I have the basics of the structure created in the mexFunction, but its getting a little confusing when a field in the structure is an array and i want to populate each element in this array as the ‘for’ loop iterates.
....
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);
}
}
....
The fields “array1” and “array2” are both vectors of the same size (lets say 8 elements) i am not sure how to populate each individual element. How do i set the field name by array index ‘j’.
To aid in the explanation, this is how the structure should look:
structure is an array of 16 elements, each element has 2 fields, each field has 8 elements each
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];
...
The values are just arbitrary.
Consider the following C code:
structArrayMEX.c
The returned structure array:
The above MEX-function should be equivalent to the following MATLAB code