How can I define some initialization commands in a s-function block in Simulink depending on a declarated variable from the Matlab command window ?

2 views (last 30 days)
Hi, I have a subsystem masked in Simulink which contains initialization commands that are used depending on a declared variable from the Matlab command window (called Bcaso) - I converted my subsystem into a s-function block, which does not depend on Bcaso. I need that my s-function block can be executed depending on Bcaso. Thanks!

Accepted Answer

Seth Popinchalk
Seth Popinchalk on 2 Feb 2011
S-functions can include parameter inputs. There are examples of this for Level 2 MATLAB S-function, (msfcndemo_limintm) and C-MEX S-functions, (see sfundemos).
In a Level 2 MATLAB S-function, during "setup" set the number of dialog parameters:
block.NumDialogPrms = 3;
Anywhere you need to reference a parameter (for example) inside "InitConditions" you can access it through the block object:
block.ContStates.Data = block.DialogPrm(3).Data;
The parameters are passed into the block through the S-function block parameters, separated by commas.
For C-MEX S-functions, use ssSetNumSfcnParams in mdlInitializeSizes to set the number of parameters and ssGetSfcnParam to get a pointer to an mxArray anywhere you need to use it. C-MEX S-functions also need to do some parameter checking, so it can get a lot more complicated. See the SFCNDEMOS for examples.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!