Dynamically change the number of ports of a block

Hello everyone,
I'm looking for a way to build a Simulink block, where the number of port is defined by the user, e.g. a mask allows the user to define by using a popup the number of input port of the block.
Is there any way to do that ? I know I can define inside a s-function the number of ports
%%Register number of input and output ports
block.NumInputPorts = 1;
block.NumOutputPorts = 1;
but is that the only solution available?
thanks a lot

2 Comments

because an image is worth thousand words
take a look at that
http://blogs.mathworks.com/seth/2008/08/13/dynamic-mask-dialogs/#comment-1732
how do they dynamically update the number of input?
do They pass a parameter to a sfunction or not?
I guess they use this instruction
replace_block(gcs,'Inport','Ground','noprompt' );
right guess?

Sign in to comment.

 Accepted Answer

Have you tried setting the number of input ports based on the parameter value? For example, try something like:
block.NumInputPorts = block.DialogPrm(3).Data;

3 Comments

Thanks for the advice, I'll try but actually I have to write a L2 S-function with C I'm wondering if I can use the same instructions, same structure like Block, or I have to replace the structure Block with SimStruct *S.
I made it, here is the code :
"
% Register number of parameters
block.NumDialogPrms = 1;
% Register number of input and output ports
block.NumInputPorts = block.DialogPrm(1).Data;
block.NumOutputPorts = 1;
"
but this solution involves a L2 mfile sfucntion and I need to write a L2 C file sfunction
Any suggestion to do that, will be very appreciated
Not sure, but you could try extending the same logic to C S-functions by using similar code in mdlInitializeSizes.
const mxArray* prm1 = ssGetSFcnParam(S, 0);
int_T nInputPorts = (int_T)(mxGetPr(prm1)[0]); //assuming param is a double value
if (!ssSetNumInputPorts(S,nInputPorts)) return;

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!