2016-04-27 16 views
2

私はこのC構造を有する:mexファイルに構造を作成するには?

struct position 
{ 
    float x, y, z; 
}; 

struct orientation 
{ 
    float x, y, z; 
}; 

struct robot 
{ 
    position pose; 
    orientation or; 
}; 

そしてメインに、私はちょうど

構造体のロボットのデータを使用し

質問は、私はMEX関数でその構造を作成するにはどうすればよいです? おかげ

EDIT 1: 私は.MATファイルで達成したいことはこれです:

robot <1x5 struct> 
    robot(1,1) <1x1 struct> 
    robot(1,1).position <1x1 struct> 
     x 
     y 
     z 
    robot(1,1).orientation <1x1 struct> with x,y and z fields 
     x 
     y 
     z 
    robot(1,2) <1x1 struct> 
    robot(1,2).position <1x1 struct> with x,y and z fields 
     x 
     y 
     z 
    robot(1,2).orientation <1x1 struct> with x,y and z fields 
     x 
     y 
     z 
        . 
        . 
        . 
    robot(1,5) <1x1 struct> 
    robot(1,5).position <1x1 struct> with x,y and z fields 
     x 
     y 
     z 
    robot(1,5).orientation <1x1 struct> with x,y and z fields 
     x 
     y 
     z 

私は私が必要とする構造を得ることができました。私が見ることができる

double values[5] = {1,2,3,4}; //Just for testing. 
const char *field_robot[] = {"pos", "or"}; 
const char *field_coordinates[] = {"x", "y", "z"}; 
mxPos = mxCreateStructMatrix(1,1,3, field_coordinates); 
mxOr = mxCreateStructMatrix(1,1,3, field_coordinates); 
mxRobot = mxCreateStructMatrix(1,1,5, field_robot); 

for(i=0; i<5; i++) 
{ 
    mxSetFieldByNumber(mxPos, 0, 0, mxCreateDoubleScalar(values[i])); 
    mxSetFieldByNumber(mxRobot, i, 0, mxPos); 
} 

をmatlabでそれは私が望む方法ですが、robot.pos.xの中で私はすべての値に対して4つしか持っていません。最後の値を保存するだけです。

+0

は、あなたが戻って構造を通過したいですか:あなたはMathWorks社のサイトでオンラインドキュメントを見つけることができます

edit([matlabroot '/extern/examples/refbook/phonebook.c']); 

:あなたはコマンドでMATLABでソースファイル全体を表示することができますMatlab?あるいは、関数呼び出しの中間段階でこれらの構造体を使用したいだけですか? –

+0

これらの構造を中間段階で使用したいと考えています。私は、MATファイルAPI関数を使用して、 'mex'変数を.matファイルに格納したいと考えています。 'matFut * pmat'と' mxArray * pa'を宣言すると、 'memPpy(void *)(mxGetPr(pa))、(void *)data、sizeof(data))'と 'matPutVariable(pmat、" LocalDouble "、pa)'。 – aripod

答えて

1

以下のコードは、MATLABビルトインの例 "phonebook.c"のコードで、MeXファイルにMATLAB構造体配列を作成する方法の例を示しています。

http://www.mathworks.com/help/matlab/matlab_external/passing-structures-and-cell-arrays.html?refresh=true#zmw57dd0e20943

const char **fnames;  /* pointers to field names */ 
const mwSize *dims; 
mxArray *tmp, *fout; 
char  *pdata=NULL; 
int  ifield, nfields; 
mxClassID *classIDflags; 
mwIndex jstruct; 
mwSize  NStructElems; 
mwSize  ndim; 

/* allocate memory for storing pointers */ 
fnames = mxCalloc(nfields, sizeof(*fnames)); 
/* get field name pointers */ 
for (ifield=0; ifield< nfields; ifield++){ 
    fnames[ifield] = mxGetFieldNameByNumber(prhs[0],ifield); 
} 
/* create a 1x1 struct matrix for output */ 
plhs[0] = mxCreateStructMatrix(1, 1, nfields, fnames); 
mxFree((void *)fnames); 
ndim = mxGetNumberOfDimensions(prhs[0]); 
dims = mxGetDimensions(prhs[0]); 
for(ifield=0; ifield<nfields; ifield++) { 
    /* create cell/numeric array */ 
    if(classIDflags[ifield] == mxCHAR_CLASS) { 
     fout = mxCreateCellArray(ndim, dims); 
    }else { 
     fout = mxCreateNumericArray(ndim, dims, classIDflags[ifield], mxREAL); 
     pdata = mxGetData(fout); 
    } 
    /* copy data from input structure array */ 
    for (jstruct=0; jstruct<NStructElems; jstruct++) { 
     tmp = mxGetFieldByNumber(prhs[0],jstruct,ifield); 
     if(mxIsChar(tmp)) { 
      mxSetCell(fout, jstruct, mxDuplicateArray(tmp)); 
     }else { 
      mwSize  sizebuf; 
      sizebuf = mxGetElementSize(tmp); 
      memcpy(pdata, mxGetData(tmp), sizebuf); 
      pdata += sizebuf; 
     } 
    } 
    /* set each field in output structure */ 
    mxSetFieldByNumber(plhs[0], 0, ifield, fout); 
} 
mxFree(classIDflags); 
+0

私はそれを発見し、最初の1x1構造体「ロボット」を作成することができましたが、今では構造体である「ロボット」、「位置」、および「向き」内に2つの新しい構造を作成する必要があります.....それが私が直面している問題です。 – aripod

+0

私はバイナリファイルに保存し、matlabにロードするべきですか?入れ子構造ですか? – aripod

関連する問題