MATLAB: Is there an example of dynamically sized input ports with different widths in a C-MEX S-function

c - mexdynamicallyinputmdlsetinputportwidthportss-functionsimulinksizedsssetinputportwidth

I would like to know if there is an example of using dynamically sized input ports with different widths in a C-MEX S-function.

Best Answer

Here is an example showing how to write a C-MEX S-function with dynamically sized input ports that accepts 1-D (vector) signals with different widths in Simulink 6.2 (R14SP2).
In Level 2 S-functions you need to use the two new methods: mdlSetInputPortWidth and mdlSetOutputPortWidth. This is described in the S-function section of the Using Simulink User's Guide.
If you open up the attached S-function source file, you will notice the following lines after mdlInitializeSizes:
#if defined(MATLAB_MEX_FILE)
# define MDL_SET_INPUT_PORT_WIDTH
static void mdlSetInputPortWidth(SimStruct *S, int_T port,
int_T inputPortWidth)
{
ssSetInputPortWidth(S,port,inputPortWidth);
}
# define MDL_SET_OUTPUT_PORT_WIDTH
static void mdlSetOutputPortWidth(SimStruct *S, int_T port,int_T outputPortWidth)
{
ssSetOutputPortWidth(S,port,outputPortWidth);
}
#endif
These methods are called with the candidate widths for dynamically sized ports. If the proposed widths are acceptable, the methods proceed to set the actual ports widths using ssSetInputPortWidth and ssSetOutputPortWidth.