MATLAB: Am I unable to generate code for a Simulink model with a while subsystem using Real-Time Workshop

codegenerategenerationmodelreal timertwsimulinksimulink codersubsystemunablewhileworkshop

I receive the following error when I try to generate code for a model with a while subsystem using Real-Time Workshop:
### Building while_bug: make -f while_bug.mk ADD_MDL_NAME_TO_GLOBALS=1
gcc -c -O -ffloat-store -fPIC -DUSE_RTMODEL -DMODEL=while_bug -DRT
-DNUMST=1 -DTID01EQ=0 -DNCSTATES=0 -DUNIX -DMT=0 -DHAVESTDIO -I. -I..
-I/sandbox/awarlock/Aslrtw/matlab/simulink/include
-I/sandbox/awarlock/Aslrtw/matlab/extern/include
-I/sandbox/awarlock/Aslrtw/matlab/rtw/c/src
-I/sandbox/awarlock/Aslrtw/matlab/rtw/c/libsrc
-I/sandbox/awarlock/Aslrtw/matlab/rtw/c/src/ext_mode/common
-I/home/awarlock/temp -I/home/awarlock/temp/while_bug_grt_rtw
-I/sandbox/awarlock/Aslrtw/matlab/rtw/c/libsrc AtomicSys.c
gcc -c -O -ffloat-store -fPIC -DUSE_RTMODEL -DMODEL=while_bug -DRT
-DNUMST=1 -DTID01EQ=0 -DNCSTATES=0 -DUNIX -DMT=0 -DHAVESTDIO -I. -I..
-I/sandbox/awarlock/Aslrtw/matlab/simulink/include
-I/sandbox/awarlock/Aslrtw/matlab/extern/include
-I/sandbox/awarlock/Aslrtw/matlab/rtw/c/src
-I/sandbox/awarlock/Aslrtw/matlab/rtw/c/libsrc
-I/sandbox/awarlock/Aslrtw/matlab/rtw/c/src/ext_mode/common
-I/home/awarlock/temp -I/home/awarlock/temp/while_bug_grt_rtw
-I/sandbox/awarlock/Aslrtw/matlab/rtw/c/libsrc WhileSys.c
WhileSys.c: In function `while_bug_WhileSys':
WhileSys.c:46: `rt_elapseTime' undeclared (first use in this function)
WhileSys.c:46: (Each undeclared identifier is reported only once
WhileSys.c:46: for each fu

Best Answer

This bug has been fixed for Release 14 SP1 (R14SP1). For previous releases, please read below for any possible workarounds:
This has been verified as a bug in Real-Time Workshop in the way that it handles
code generation for a model with a 'while subsystem'.
You are likely to encounter this issue if the model satisfies the following conditions:
1. Iterator subsystem containing another atomic subsystem
2. An input to the atomic subsystem is also a canonical input to the iterator subsystem
3. The Atomic subsystem does not require output and update to be combined
4. At least one of the sources of the Atomic subsystem executes after the atomic subsystem
5. Atomic subsystem does not have direct feed through
Currently, to work around this issue, try adding a Gain block between the atomic subsystem and the input port of the iterator subsystem so it will no longer be a canonical input to the iterator system.