Ich arbeite mit Scilab und ich benutze MexFunctions einschließlich mex.h Header-Datei. Ich möchte hier eine Struktur in einer Struktur erstellen. Hier ist mein Code:mexFunktionen: Struktur in einer Struktur
#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;
}
Ausgang:
-->a=mex_5292()
a =
mylong: 987987
Nested: [0x0 constant]
myarraydouble: [987,654,321]
Was ich will jetzt ist eine neue Struktur in der "Nested" Feld zu speichern. Mein Code ist:
// 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);
Ausgang: (i mit einem (1) auf jeden Wert zugreifen können, a (2), eine (3))
-->a=mex_5292()
a =
inval1: 12
inval3: 34
inval2: "hello"
Jetzt versuche ich hinzufügen Diese 1x1-Struktur in meinem geschachtelten Feldnamen.
mxSetField(struct_array_ptr5_1, 0, "Nested", s);
, aber ich habe immer einen Segmentierungsfehler oder etwas ähnliches:
Ausgang mit der Zeile:
-->a=mex_5292()
a =
mylong: 987987
Nested: [1x1 struct]
myarraydouble: [987,654,321]
Und
-->a.Nested
ans =
inval1: unknown
inval3: unknown
inval2: unknown
Ich kann nicht auf Werte zugreifen . Wenn mir jemand helfen könnte. Ich denke, das Problem kommt von mxSetField, vielleicht ist es nicht für Struct in Struct implementiert.
Können wir ein minimales Beispiel dafür bekommen, wo Sie Probleme haben? – UKMonkey
Warum verwenden Sie 'mxDestroyArray (inval1);', ...? –