1

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.

2
  • What do mean "how to work with this struct"? Are you interested in how to use a struct at all? Commented Aug 31, 2015 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. Commented Aug 31, 2015 at 2:59

3 Answers 3

4

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.

2
  • Thanks a lot! This is exactly what i was looking for. Commented Aug 31, 2015 at 20:51
  • @user4009004, you're quite welcome, glad to hear it! Commented Aug 31, 2015 at 20:59
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.

1
  • 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. Commented Aug 31, 2015 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.