I’m trying to create a level 2 S-Function written in C that has different numbers of output ports depending on the signals’ datatypes that are connected to the inports.
The practical background is that I have some other S-Functions that define custom datatypes by calling ssRegisterDataType. These datatypes describe pointers to certain data.
I now want to have another S-Function that can consume these custom datatypes (pointers) and, depending on what pointer type is fed into them, output certain data.
I tried setting the number of output ports through ssSetNumOutputPorts by
static void mdlSetInputPortDataType(SimStruct *S, int portIndex,DTypeId dType)
{
if( portIndex == 0 )
{
if( dType == ssGetDataTypeId(S, "ptrtype1" ) )
{
if (!ssSetNumOutputPorts(S, nOutportsPtr1)) return;
}
else if( dType == ssGetDataTypeId(S, "ptrtype2" ) )
{
if (!ssSetNumOutputPorts(S, nOutportsPtr2)) return;
}
else if( dType == ssGetDataTypeId(S, "ptrtype3" ) )
{
if (!ssSetNumOutputPorts(S, nOutportsPtr3)) return;
}
else if( dType == ssGetDataTypeId(S, "ptrtype4" ) )
{
if (!ssSetNumOutputPorts(S, nOutportsPtr4)) return;
}
else
{
ssSetErrorStatus(S, "Input data type is not supported."); return;
}
if (!ssSetInputPortDataType(S, portIndex, dType)) return;
}
} /* mdlSetInputPortDataType */
while in mdlInitializeSizes I set the inport’s data type to be DYNAMICALLY_TYPED.
But Simulink keeps telling me I should set the inport data type in mdlSetInputPortDataType on a call to ssSetNumOutputPorts. I assume inside this function only the data type of inports may be changed.
Does anybody know of a solution to this problem? The only workaround I can think of is masking the S-Function and having the user to manually select the pointer type that he wishes to be processed. With data type propagation this seems messy and unnecessary though.
You need to set number of ports in mdlInitializeSizes. You cannot change that after that function. The only workaround is the one you mentioned.