variable sized inputs/outputs from rtwsfcn S-Function

7 views (last 30 days)
Hello everyone,
i have a submodel which i want to use in several bigger models. To protect the model I want to compile it as an S-Function via the rtwsfcn.tlc option (I can't use protected Submodels for various reasons, i need to use the S-Function).
Because the S-Function should be used in various models it needs to have Inputs which are dynamically sized. Sadly, i can't figure out how i can do that. Everytime i compile it, the inputs and outputs of the generated S-Function have a fixed Portwidth of 1.
I've made a minimal model (sfun_dynamic.slx). When I compile it with these settings and try to run it with Arrays as inputs attached to it, the model doesn't run (sfun_implemented.slx)
I get the following Error Code:
"
Error in port widths or dimensions. Output port 1 of 'sfun_implemented/Constant' is a one dimensional vector with 4 elements.
Component:Simulink | Category:Model error
Error in port widths or dimensions. Input port 1 of 'sfun_implemented/S-Function' is a one dimensional vector with 1 elements.
Component:Simulink | Category:Model error
"
Is there any possibility to make the input and output port widths dynamically sized?
Thank you very much in advance for every advise or idea you have.

Answers (1)

Manoj Mirge
Manoj Mirge on 24 Mar 2023
Hi Jan,
You can write your S-function to support arbitrary input dimensions. In that case, the Simulink engine determines the actual input dimensions when the simulation is started by evaluating the dimensions of the input vectors driving the S-function.
A C MEX S-function and Level-2 MATLAB S-function can have multiple input and output ports and each port can have different dimensions. The number of dimensions and the size of each dimension can be determined dynamically.
You can refer below link to know how to configure C MEX S-Functions to accept dynamic sized inputs :
You can read about SetPreCompInpPortInfoToDynamic method using below link :
Hope this helps.

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!