2016-10-07 15 views
0

私はScilabで作業しています。mex.hヘッダファイルを含むmexFunctionsを使用しています。 私がここで望むのは、構造内に構造を作成することです。 は、ここに私のコードです:mexFunctions:構造体内の構造

#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] 

何私が今したいことは「ネスト」フィールドに新しい構造を格納することです。私のコードは次のとおりです。

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

出力:今、私は追加しようとしている

-->a=mex_5292() 
a = 

inval1: 12 
inval3: 34 
inval2: "hello" 

(私は(1)、(2)、(3)で各値にアクセスすることができます)私の入れ子のフィールド名のこの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のために実装されていない。

+2

問題が発生している場所の最小限の例はありますか? – UKMonkey

+1

なぜ 'mxDestroyArray(inval1);'、...? –

答えて

0

私は質問に答えました: これはScilabでは実装されていないが、MATLABでも同じコードが動作するからです。