2016-04-27 12 views
2

Ich habe diese Struktur C:Wie erstellt man Strukturen in Mex-Dateien?

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

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

struct robot 
{ 
    position pose; 
    orientation or; 
}; 

Und im Haupt nur ich

struct Roboterdaten

Die Frage ist, wie erstelle ich diese Struktur in einer mex Funktion ? Dank

EDIT 1: Was will ich in der .mat Datei erreichen, ist dies:

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 

Ich war in der Lage, die Struktur bekommen, die ich brauche:

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

Ich kann sehen, in Matlab ist es wie ich will, aber in robot.pos.x habe ich nur 4 für alle Werte. Es speichert nur den letzten Wert.

+0

Haben Sie die Struktur übergeben wollen zurück zu Matlab? Oder wollen Sie diese Strukturen nur in einem Zwischenschritt eines Funktionsaufrufs verwenden? –

+0

Ich möchte diese Strukturen in einem Zwischenschritt verwenden. Ich möchte die MAT-File API-Funktionen verwenden, um die 'Mex' Variablen in einer .mat Datei zu speichern. Wenn Sie 'MATFile * pmat 'und' mxArray * pa' angeben, rufen Sie 'memcpy ((void *) (mxGetPr (pa)), (void *) Daten, sizeof (data))' und nach 'matPutVariable (pmat," LocalDouble ", pa)'. – aripod

Antwort

1

Der folgende Code stammt aus dem integrierten MATLAB-Beispiel "phonebook.c" und zeigt ein Beispiel für das Erstellen eines MATLAB struct-Arrays in einer MeX-Datei. Sie können die gesamte Quelldatei in MATLAB mit dem Befehl anzuzeigen:

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

Sie Online-Dokumentation auf der MathWorks-Website finden:

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

Ich habe das gefunden und ich konnte den 'ersten' 1x1 struct "Roboter" erstellen, aber jetzt muss ich zwei neue Strukturen in "robot", "position" und "orientation" erstellen, die auch Strukturen sind ..... Das ist das Problem, vor dem ich stehe. – aripod

+0

Soll ich es lieber in einer Binärdatei speichern und in Matlab laden? Wird es die verschachtelten Strukturen haben? – aripod