Creating a structure within a structure in C++
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi
I have the following problem: I want to create a mxstructure within a structure. I want to create a single matlab structure in the c++ environment with varying number of fields and field names. When using the mxCreateStructArray function one is limited to the following properties: All structs in the array have the same number of fields. All structs have the same field names.
In Matlab it is possible to create a structure as follows:
a.b.field1="data"; a.b.field2="data2"; a.c.field1="data1";
How can I do this with the C/C++ Matrix Library in c++? Or is it not possible
Thank you for your help. Joachim
4 comentarios
James Tursa
el 24 de Jul. de 2013
Pretend for the sake of exercise that sie_get_name is an m-function. If you were to write your overall code at the m-file level, what would it look like?
Joachim Stallmann
el 25 de Jul. de 2013
Editada: Joachim Stallmann
el 25 de Jul. de 2013
Respuestas (1)
James Tursa
el 23 de Jul. de 2013
Editada: James Tursa
el 23 de Jul. de 2013
Some comments about the m-file code:
a.b.field1="data";
Creates a structure 'a' with one field, 'b'. Also 'b' gets created as a structure with one field 'field1'. So you end up with 'a' being a 1x1 struct with one field, and 'b' being a 1x1 struct with one field. And there is a variable stored in a.b.field1.
a.b.field2="data2";
Causes MATLAB to reallocate the struct memory of 'b' to make room for a new field 'field2'. And a variable is stored in the new field.
a.c.field1="data1";
Causes MATLAB to reallocate the struct memory of 'a' to make room for the new field 'c'. Also 'c' gets created as a struct with one field 'field1'. And a variable is stored in this new field.
In a mex routine, you can accomplish the same thing with the mxAddField and mxSetField (or mxSetFieldByNumber) functions. Of course, it is simpler if you know all the field names up front so that the mxAddField steps can be avoided.
If you are having problems with specific code you have written you will need to post it so we can have a look at it. In particular, you will need to explain in more detail what you mean by the statements "All structs in the array have the same number of fields. All structs have the same field names". E.g., maybe give an example of a struct array at the m-file level that you are having trouble building at the C++ level.
0 comentarios
Ver también
Categorías
Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!