MATLAB: Are signal dimensions not calculated correctly connecting the dynamically sized output from the C-Mex S-Function to a Demux block in Simulink

c - mexdemuxdynamicdynamicallyerrormuxports-functionsimulinksize;sized

Why are signal dimensions not calculated correctly when connecting the dynamically sized output from my C-MEX S-Function to a Demux block in Simulink?
I have a C-MEX S-Function that has a fixed input size of 1 and a dynamically sized output. I am connecting the output of my S-Function block to a Demux block. This is then connected to 3 other blocks, each expecting one input.
When I try to run my model I get the following error:
"Invalid setting for input port dimensions of Demux block 'selfcncall_test1/Demux'.
The dimensions are being set to 1.
This is not valid because the input has fewer elements than
the total number of output ports of this block"
and the output of my C-MEX S-Function has dimension 1 instead of 3.
This does not happen with no input to the S-Function block, if my S-Function block input is dynamically sized, or if I connect the output of my S-Function block to any block other than a Mux/Demux.

Best Answer

When you specifically declare the input port dimensions, Simulink never calls the mdlSetInputPortWidth function and the dimensions of your output port width are never set.
To work around this problem, set the input port dimensions to dynamically sized. Then in the mdlSetInputPortWidth, you can specify that the input port has a dimension of 1 and can properly size your output port.
Alternately insert a Signal Specification block between the S-Function block and the Demux block, setting the signal width to the proper dimension.