MATLAB: How to create a variable sized output signal from a C MEX S function block in Simulink 7.6 (R2010b)

simulink

I have two variable sized inputs that are fed into a C MEX S function block. I would like to create a variable sized output signal from the same block. I would like to implement this in my S function code. I run into some generic error messages when I attempt to create a variable sized signal in an S function block, which do not a clear explanation about how to fix my code. The error messages are as follows:
Simulink cannot propagate the variable-size mode from the output port 1 of an input block to the input port 2 of output block. This input port expects a fixed-size mode. Examine the configuration of the final output block for one of the following scenarios:
1) the block does not support variable-size signals;
2) the block supports variable-size signals but needs to be configured for them.

Best Answer

You can set the output port in an S function block within the 'mdlSetInputPortWidth' function using the 'ssSetOutputPortWidth' function when you have a variable sized signal feeding the S function block. In the attached example 'test.mdl', this scenario is well illustrated.
In order to execute the attached model, follow the steps below:
1. Download the attached 'test.mdl' and 'varsize_output_fcn.cpp' files to your current working directory.
2. Choose a compiler to compile the C MEX S function by executing the command 'mex -setup' in the MATLAB command line.
3. MEX the S function code using the command 'mex varsize_output_fcn.cpp'.
4. After step 3, run the model to see a variable sized output port from the S function block.