Creating a structure within a structure in C++

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 commentaires

Martin: Are you saying that the MATLAB code you wrote creates a an array of structures with different field names? Because the code:
a.b.field1="data"; a.b.field2="data2"; a.c.field1="data1";
Still creates a structure with fields 'a', 'b' and 'c', which each in turn are structures. I don't see the variability in field names?
Hi, thanks for the help, it is greatly appreciated.
I want to create a structure with fields 'a','b' and 'c'. Where 'a','b' and 'c' are structures again. This is necessary because the structures 'a','b' and 'c' have different internal field names. As far as I can tell I can only create a structure array in c++, and then then i am limited that all structures in the array must have the same field names. I am trying to write a program that reads a data file that contains data from various measurement devices. The file I am reading is a binary SIE file. My c++ code might explain it better, I am tying to code c++ code for the first time so please be patient:
//Start the MATLAB engine
Engine *ep = 0;
mxArray *Parent_struct = NULL;
const char *field_names[]={"ChannelName"};
int number_of_channels=5;
int NUMBER_OF_FIELDS=1;
mwSize dims[2] = { number_of_channels ,1};
Parent_struct = mxCreateStructArray(2, dims, NUMBER_OF_FIELDS, field_names);
I then populate the channel name fields using a loop, it calls a sie_get_name function to return a string of the channel:
field_num=mxGetFieldNumber(Parent_struct,field_names[0]);
mxArray *field_channel_name;
channel_name = mxCreateString(sie_get_name(channel)); mxSetFieldByNumber(Parent_struct,index_of channel,field_num,channel_name);
This section is working fine no problems there. The problems is that the measured data is different for every channel, which requires different field names for every channel structure. For example channel one is a time history while channel 2 has pictures stored in it. I can add fields to the structure, but then all structures have the same fields. I can control to which structures I send data, but the field is still there, empty though .
char *measurment_field="Measurment";
mxAddField(Parent_struct,measurment_field);
field_ind_measure=mxGetFieldNumber(Parent_struct,measurment_field);
mxSetFieldByNumber(Parent_struct,index_of_channel,field_ind_measure,data_form_channel);
//Copy struct to Matlab workspace and close the Matlab engine
ngPutVariable(ep,"Name_of_parent_struct",Parent_struct);
engClose(ep);
I essence I created and send a structure Parent_struct<5x1> to Matlab where the data of channel one is found in Parent_struct(1,1) from channel two in Parent_struct(2,1) etc.
It would be great if I could create a structure for every channel within the parent structure, within c++, eg Parent_struct<1x1>. Where the data of the channels stored in different "sub" structures,Parent_struct.Channel_1<number_of data_fields_channel_1,1>, Parent_struct.Channel_2<number_of data_fields_channel_2,1> etc. I could however not find a way to do this. The only solution I found is to create a new structure for every channel in c++, send them to the Matlab workspace and then write a program that combines them again, its a bit tedious and makes the program more complex and one requires the Matlab code plus the c++ code to get it to run.
Thanks again for your help
James Tursa
James Tursa le 24 Juil 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 le 25 Juil 2013
Modifié(e) : Joachim Stallmann le 25 Juil 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

Connectez-vous pour commenter.

Réponses (1)

James Tursa
James Tursa le 23 Juil 2013
Modifié(e) : James Tursa le 23 Juil 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.

Catégories

En savoir plus sur Write C Functions Callable from MATLAB (MEX Files) dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by