0

I used matlab coder to convert a matlab code to C. The simple array I had defined in matlab got converted to a complicated struct in C.

struct emxArray_real_T
{
  double *data;
  int *size;
  int allocatedSize;
  int numDimensions;
  boolean_T canFreeData;
};

It would be of great help if anyone can shed any light on how to work with this struct.

user4009004
  • 13
  • 1
  • 3
  • What do mean "how to work with this struct"? Are you interested in how to use a struct at all? – SirPython Aug 31 '15 at 02:42
  • 1
    I reccomend reading the documentation: [C Code Interface for Arrays](http://www.mathworks.com/help/fixedpoint/ug/c-code-interface-for-unbounded-arrays-and-structure-fields.html). If that doesn't answer your question then you should edit it to be more specific. – horchler Aug 31 '15 at 02:59

3 Answers3

3

The type emxArray_real_T is created because MATLAB Coder was unable to determine a fixed size or sufficiently small bounds on the size for your array in MATLAB, like 2x3. In this case the generated code allows the size of the array to vary at runtime by using dynamic memory allocation (e.g. malloc) and this data structure to represent your MATLAB array in C. These are called dynamically allocated variable size arrays in the MATLAB Coder documentation and the struct fields are used to track the dynamically allocated memory in the generated code.

Regarding working with such arrays, the documentation recommended by @horchler is a good place to start. Also, you can have a look at this other answer that describes the basics of working with the same struct when the stored data is uint32 rather than double or real_T in your case.

Community
  • 1
  • 1
Ryan Livingston
  • 1,898
  • 12
  • 18
2

An Array is a collection of same data type whereas a structure is a collection of different data types.

In Matlab an array can contain variables of all data types.

That's why when you are trying to convert from Matlab array to C array it is converting to a C structure instead of a C array because your Matlab array comprises of different data type.

Mahendra Yadav
  • 535
  • 6
  • 16
  • `emxArray` data types are used when a static size or sufficient static bounds on the size of the corresponding MATLAB array are not able to be computed. They do not permit the datatype to change for a given array. You will notice that the data pointer is declared `double*` meaning that the array will contain only data of type `double`. – Ryan Livingston Aug 31 '15 at 13:08
0

A struct in the C programming language (and many derivatives) is a complex data type declaration that defines a physically grouped list of variables to be placed under one name in a block of memory, allowing the different variables to be accessed via a single pointer, or the struct declared name which returns the same address. The struct can contain many other complex and simple data types in an association.

Thus a Struct in C is basically an array of physically grouped list of variables in it.

If you really wish to use only a simple array look here for its basic working and syntax. And here for struct and its basic working and syntax.

Riddhesh Sanghvi
  • 1,218
  • 1
  • 12
  • 22