cgsl_0202: Usage of For, While, and For Each subsystems with vector signals

ID: Titlecgsl_0202: Usage of For, While, and For Each subsystems with vector signals
DescriptionWhen developing a model for code generation,
AUse For, While, and For Each subsystems for calculations that require iterative behavior or operate on a subset (frame) of data.
BAvoid using For, While, or For Each subsystems for basic vector operations.
RationaleA, BAvoid redundant loops.
See Also
Last ChangedR2010b
Examples

The recommended method for preceding calculation is to place the Gain block outside the For Subsystem. If the calculations are required as part of a larger algorithm, you can avoid the nesting of for loops by using Index Vector and Assignment blocks.

Recommended

for (s1_iter = 0; s1_iter < 10; s1_iter++) {
  RecommendedOut[s1_iter] = 2.3 * vectorInput[s1_iter];
}

A common mistake is to embed basic vector operations in a For, While, or For Each subsystem. The following example includes a simple vector gain inside a For subsystem, which results in unnecessary nested for loops.

Not Recommended

for (s1_iter = 0; s1_iter < 10; s1_iter++) {
  for (i = 0; i < 10; i++) {
    NotRecommendedOut[i] = 2.3 * vectorInput[i];
  }
}    

Was this topic helpful?