Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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.

share|improve this question
    
What do mean "how to work with this struct"? Are you interested in how to use a struct at all? – SirPython Aug 31 at 2:42
1  
I reccomend reading the documentation: C Code Interface for Arrays. If that doesn't answer your question then you should edit it to be more specific. – horchler Aug 31 at 2:59
up vote 2 down vote accepted

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.

share|improve this answer
    
Thanks a lot! This is exactly what i was looking for. – user4009004 Aug 31 at 20:51
    
@user4009004, you're quite welcome, glad to hear it! – Ryan Livingston Aug 31 at 20:59

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.

share|improve this answer
    
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 at 13:08

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.