我正在与Scilab合作,并且我正在使用mexFunctions,包括mex.h头文件。 我想要的是在结构中创建一个结构。 这是我的代码:
#include <mex.h>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs,
const mxArray* prhs[])
{
#define rows5_1 1
#define cols5_1 1
#define TOTAL_ELEMENTS5_1 (rows5_1 * cols5_1)
int ndim5_1 = 2, dims5_1[2] = {rows5_1, cols5_1};
int number_of_fields5=1;
const char *field_names5_1[] = {"mylong","Nested","myarraydouble"};
double mylong[] = {987987};
mxArray *Nested;
double myarraydouble[] = {987,654,321};
double *pr5;
mxArray *field_value5, *struct_array_ptr5_1;
struct_array_ptr5_1 = mxCreateStructMatrix(1, 1, 3, field_names5_1);
int index5;
// Populate 1 mylong double
for (index5=0; index5<1; index5++) {
field_value5= mxCreateDoubleMatrix(1, 1, mxREAL);
pr5 = mxGetPr(field_value5);
pr5[0] = mylong[index5];
mxSetField(struct_array_ptr5_1, index5, "mylong",field_value5);
mxDestroyArray(field_value5);
}
// Populate the array myarraydouble
for (index5=0; index5<1; index5++) {
field_value5= mxCreateDoubleMatrix(1, 3, mxREAL);
pr5 = mxGetPr(field_value5);
for(int i=0; i<3; i++) {
pr5[i] = myarraydouble[i];
}
mxSetField(struct_array_ptr5_1, index5, "myarraydouble", field_value5);
mxDestroyArray(field_value5);
}
if (nlhs)
plhs[0] =struct_array_ptr5_1;
}
输出:
-->a=mex_5292()
a =
mylong: 987987
Nested: [0x0 constant]
myarraydouble: [987,654,321]
我现在想要的是在&#34;嵌套&#34;中存储一个新结构。领域。我的代码是:
// Populate the struct
mxArray *s;
#define rows5 1
#define cols5 3
int dims5[3]= {rows5,cols5};
const char *field_names5_2[] = {"inval1","inval3","inval2"};
const char *inval2_names[]= {"hello"};
double inval1_names[] = {12};
double inval3_names[] = {34};
s = mxCreateStructMatrix(1, 1, 3, field_names5_2);
mxArray *inval1,*inval2,*inval3;
//inval1
inval1= mxCreateDoubleMatrix(1, 1, mxREAL);
pr5 = mxGetPr(inval1);
pr5[0] = inval1_names[0];
mxSetField(s, 0, "inval1", inval1);
//Inval3
inval3= mxCreateDoubleMatrix(1, 1, mxREAL);
pr5 = mxGetPr(inval3);
pr5[0] = inval3_names[0];
mxSetField(s, 0, "inval3", inval3);
//Inval 2 //string
inval2 = mxCreateString(inval2_names[0]);
mxSetField(s, 0, "inval2", inval2);
mxDestroyArray(inval1);
mxDestroyArray(inval2);
mxDestroyArray(inval3);
输出:(我可以用(1),a(2),a(3))访问每个值
-->a=mex_5292()
a =
inval1: 12
inval3: 34
inval2: "hello"
现在我试图在我的嵌套字段名称中添加这个1x1结构。
mxSetField(struct_array_ptr5_1, 0, "Nested", s);
但我总是有分段错误或类似的东西:
输出行:
-->a=mex_5292()
a =
mylong: 987987
Nested: [1x1 struct]
myarraydouble: [987,654,321]
并且
-->a.Nested
ans =
inval1: unknown
inval3: unknown
inval2: unknown
我无法访问价值观。如果有人可以帮助我。我认为问题来自mxSetField,也许它没有在struct中实现struct。
答案 0 :(得分:0)
我对我的问题有一个答案: 这是因为它没有在Scilab上实现,但相同的代码适用于MATLAB。