What's a less repetitive way to convert c structs to matlab structs? -
i have c structure need convert matlab structure. fields have different types, , arrays. i'm looking way has minimum boilerplate/repeated information.
typedef struct { uint32 time ; double px[2]; } data_s; data_s s; mxarray *fieldval , *structarrayptr; structarrayptr = mxcreatestructmatrix(1, 1, numfields, fieldnames); i'm trying replace following, repetitive.
fieldval = mxcreatenumericmatrix(1, 1, mxuint32_class, mxreal); mxgetdata(fieldval)[0] = s.time; mxsetfield(structarrayptr, 0, "time", fieldval); fieldval = mxcreatenumericmatrix(1, 2, mxdouble_class, mxreal); mxgetdata(fieldval)[0] = s.px[0]; mxgetdata(fieldval)[1] = s.px[1]; mxsetfield(structarrayptr, 0, "px", fieldval); the idea this.
typedef struct { char *name ; int type ; int arity; } fields_s; const fields_s fields[] = { {"time", mxuint32_class, 1}, {"px" , mxdouble_class, 2} }; void *tmp; (i = 0; < numfields; i++) { fieldval = mxcreatenumericmatrix(1, fields[i].arity, fields[i].type, mxreal); switch (i) { case 0: tmp = &(s.time); break; case 1: tmp = s.px ; break; // don't take address of arrays } // hmm, can't sizeof() on dereferenced void*. // solution other repeating field name? // , make bad assumptions data representation being same? memcpy(mxgetdata(fieldval), tmp, sizeof(*tmp) * fields[i].arity); mxsetfield(structarrayptr, 0, fields[i].name, fieldval); } even if work, unsatisfying because have manually line field names , have use different syntax depending on whether each array.
Comments
Post a Comment