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

3 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:
&(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?

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:
<https://in.mathworks.com/help/ecoder/ug/design-custom-storage-classes-and-memory-sections.html>
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.
  1 Comment
TAB
TAB on 20 Jul 2018
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Deployment, Integration, and Supported Hardware in Help Center and File Exchange

Products


Release

R2010b

Community Treasure Hunt

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

Start Hunting!