Specifying the number of Output ports based on the input port values in the simulink S-function

Hello
Is there any way to specify the number of output ports based on the input port values in Simulink. I tried something as below.
static void mdlInitializeSizes(SimStruct *S) {
ssSetNumContStates(S, 0);
ssSetNumDiscStates(S, 0);
if (!ssSetNumInputPorts(S, 1))
return; /*Setting the number of input ports*/
/* configure input on first port*/
ssSetInputPortWidth( S, 0, 1 );
ssSetInputPortDataType( S, 0, SS_INT8 );
ssSetInputPortRequiredContiguous(S, 0, true);
ssSetInputPortDirectFeedThrough(S, 0, 1);
int n , i =0;
int *number = ( int *)ssGetInputPortSignal( S, 0 );
int num = ( int ) *number;
if(num==45)
n = 1;
else
n =0;
/* configure output ports*/
if ( !ssSetNumOutputPorts( S, n ) )
return;
}
When I try to mex the above file, it compiles but then when I try to implement in the Simulink then the MATLAB crashes(an error report is generated and I need to close it). Can any one please help me with this.
Thank you
Priyanka

 Accepted Answer

I think the file crashes because the input signal data is not available in mdlInitializeSizes, since this method is called very early during model initialization, before the model even starts executing. You can the documentation here to see what sequence each of the S-function methods is called in.
In any case, it is not possible to change the number of outputs of a block based on an input signal. You may however change the number based on a non-tunable parameter (whose value is available before simulation starts). Perhaps you can explain more about why you want to change the number of outputs during simulation? You cannot change the structure of the model during simulation, so you cannot connect the new outputs to any blocks at any rate.

5 Comments

Thank you for the reply.
The reason I want to change the number of outputs during simulation is I have couple of simulink blocks(User defined),the S-function logic is same for all the blocks(So basically I just want to use only one C-S-function code). Here, only the input differs(output of another S-function block). So depending on the value of input, the logic in the S-function adjusts the output ports accordingly.
If the above is not possible, then could you please let me know if there is a way to write the output of the S-function block(1) to another C-function(or file) so that I can call that C-function(or file) in another S-function block(2)(another C-s-function) before the simulation starts and hence I can decide on the number of output ports.
Thank you
Priyanka
Priyanka: I'm not sure if your description addresses why you need the number of outputs to change during simulation. Are all the outputs of the same datatype? If yes, you may want to combine the outputs in a signal that changes its size at run-time (such signals are called variable-size signals). Please see C S-Function with Variable-Size Signals for an example.
Thank you for your reply. My datatypes are also different. Could you please tell me if there is any way to solve this issue.
Perhaps you can always register the maximum number of outputs possible for your block, and for each output have another output indicate whether there is a valid output available at that port? I know it's not an elegant solution, but I can't think of anything else. Sorry!
I thought so too. Thank you once again for your help!

Sign in to comment.

More Answers (1)

Categories

Find more on Simulink in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!