Creating a structure within a structure in C++
2 views (last 30 days)
Show older comments
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 Comments
James Tursa
on 24 Jul 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?
Answers (1)
James Tursa
on 23 Jul 2013
Edited: James Tursa
on 23 Jul 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 Comments
See Also
Categories
Find more on Structures in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!