Creating a structure within a structure in C++

2 views (last 30 days)
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
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?
Joachim Stallmann
Joachim Stallmann on 25 Jul 2013
Edited: Joachim Stallmann on 25 Jul 2013
Hi
sie_get_name just returns the channel name from the binary sie file as a sting. I could do the same when i define a sting array, of the channel names and loop over it, eg:
//define array
string[] channel_names = new string[5];
//populate array:
channel_names[0] = "channel_1";
channel_names[1] = "channel_2";
channel_names[2] = "channel_3";
channel_names[3] = "channel_4";
channel_names[4] = "channel_5";
field_num=mxGetFieldNumber(Parent_struct,field_names[0]);
mxArray *field_channel_name;
for( int index = 0; index < 5; index++ )
{
field_channel_name = mxCreateString(channel_names[index]); mxSetFieldByNumber(Parent_struct,index_of channel,field_num,channel_name);
}
My problem is not to populate the structure but rather to create a structure of the form a.b<number_of_fiels> where a and b are structures.
Martin

Sign in to comment.

Answers (1)

James Tursa
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.

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!