MATLAB Answers

0

Why are 2D arrays in Simulink represented as 1D arrays in the generated C code in Real-Time Workshop 5.6 (R2010b)?

I am attempting to generate C code from my Simulink model. I have a parameter (external variable with direct access) defined in a hand-coded header file as:
SInt16_T EOLFillTimeLookup[2][2];
This parameter is a 2x2 matrix in Simulink but when I generate code, the generated code is expecting a 1D array instead of a 2D array.
For example, when passing a pointer to this parameter to an interpolate function the code uses:
&(EOLFillTimeLookup[0])
I would expect it to be:
&(EOLFillTimeLookup[0][0])
I realize these are functionally equivalent, but this is causing compiler warnings. Is there a way to tell Real-Time Workshop to treat my parameter as a 2D array?

1 Answer

Answer by MathWorks Support Team on 18 Oct 2013
 Accepted Answer

Real-Time Workshop 5.6 (R2010b) treats multidimensional data as 1-D and it serializes it such that when you access x[0], x[1], x[2] in the generated code, the generated code expects to be moving down columns and then moving to the next column. Thus multidimensional arrays are ordered in a column-major format and not a row-major format in the generated code.
To work around this issue, there are two options:
1. One option is to re-order the 2D variable definition in the hand code so that it is ordered correctly in the generated code.
2. The second option is to use the get/set custom storage class with the data. The get/set custom storage classes replace variable reads and writes with user-specified function calls. The user has to implement those function calls, but in this case, that provides a perfect place for the user to do the array access the way that he wants it.
For more information on GET SET Custom Storage Class refer to the following documentation link:
On embedded systems, the overhead of the extra function call might be unacceptable to the user in which case the first option may be used instead.

  2 Comments

Is this true in Matlab R2015b. I have simulink output or [8x12] array. But in generated generated output is 96 array.
All the work around suggested above are not useful for me. My software is based on AUTOSAR and Simulink output goes to code which is also generated by other tool.
Could you solve this problem? I am in the same shoes.

Sign in to comment.