MATLAB: How to set the output port data type in the C MEX S-function in Simulink 6.4 (R2006a)

cdatamdl_set_input_port_data_typemdloutputportdatatypemdlsetdefaultportdatatypesmdlsetinputportdatatypes-functionsimulinktype

I have two inputs to my C MEX S-function, each of different data type. How do I set the output data type of the S-function to that of the second input?

Best Answer

This can be achieved by defining the data type propagation function mdlSetInputPortDataType(), as demonstrated in the attached model and C S-function file. The S-function does the following:
1) Sets the input and output port data types to DYNAMICALLY_TYPED in mdlInitializeSizes() as follows:
ssSetInputPortDataType(S, 0, DYNAMICALLY_TYPED)
ssSetInputPortDataType(S, 1, DYNAMICALLY_TYPED)
ssSetOutputPortDataType(S, 0, DYNAMICALLY_TYPED)
2) Replaces the default input port propagation behavior by defining the function mdlSetInputPortDataType() using the associated preprocessor symbol MDL_SET_INPUT_PORT_DATA_TYPE.
When initializing the block diagram, Simulink calls mdlSetInputPortDataType() with a proposed data type, which is typically determined by the data type of the port to which this port is connected to on the other end. The function mdlSetInputPortDataType() can accept the proposed data type by setting the corresponding port to the proposed type using the macro ssSetInputPortDataType(). Furthermore, this function can also be used to reject the proposed data type or even propagate the data type to other ports.
In the example, mdlSetInputPortDataType() checks the input port number and assigns the proposed data type using macro ssSeIntputPortDataType(). For the second input port, it also assigns that data type to the output port using the ssSetOutputPortDataType() macro. As the port indices in the S-Function are zero-based, port number 1 refers to the second input port.
#define MDL_SET_INPUT_PORT_DATA_TYPE
static void mdlSetInputPortDataType((SimStruct *S,int_T port,DTypeId dataID)
{
………
………
if( 1 == port)
{
ssSetOutputPortDataType(S, 0, dataID);
}
}
3) If Simulink cannot determine what port data types to propose, it calls mdlSetDefaultPortDataTypes() to set each port to a default data type. Again, to modify the built-in behavior, both the mdlSetDefaultPortDataTypes() function and the MDL_SET_DEFAULT_PORT_DATA_TYPES preprocessor symbol need to be defined.