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

6 views (last 30 days)
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:
I would expect it to be:
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?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 15 Jun 2020
Edited: MathWorks Support Team on 15 Jun 2020
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 custom storage class with the data. The 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 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.

Sign in to comment.

More Answers (0)


Find more on Simulink Coder in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!