MATLAB: Do I not get a reusable function for a subsystem containing a Switch block, when driven by a Constant block using the Real-Time Workshop 6.0 (R14)

codegenconstantreusablereusefcnsimulink coderswitch

I have a subsytem that contains an "Inport block" feeding into a Switch block. When I connect an "Inport" block or a "Constant" block to the subsystem and generate code for a reusable function, the code generated should be independent of the input-output signature. However, I observe that seperate functions are generated for each case based on the input to the subsystem.

Best Answer

This is expected behavior in the Real-Time Workshop 6.0 (R14).
The reason two functions were being generated is because functions that appear reusable at the graphical level may not be reusable at the C-code level especially in the case of Constant values. It is within the Simulink design to generate two functions if the Input/Output signatures vary. In the case of Constant values, Simulink subsystem semantics allow the subsystem to be impacted by its context since the "Switch block" is fed a constant and the value gets hard-coded and not treated as a signal.
In order to reuse a subsystem when the input is an "Inport" block or a "Constant" block, that is, in order to treat the constant block as a signal, use a "Merge" block either within the subsystem or outside the subsystem. The "Merge" block does not create an unnecessary copy of the signal and reuses the memory location of its input and will not add any overhead to the code.