Parameterizing Composite Components
Composite component parameters let you adjust the desired parameters of the underlying member components from the top-level block dialog box when building and simulating a model.
Specify the composite component parameters by declaring a corresponding
parameter in the top-level parameters
declaration
block, and then assigning it to the desired parameter of a member
component. The declaration syntax is the same as described in Declare Component Parameters.
For example, the following code includes a Foundation library Resistor block in your custom component file, with the ability to control the resistance at the top level and a default resistance of 10 Ohm:
component MyCompositeModel [...] parameters p1 = {10, 'Ohm'}; [...] end components(ExternalAccess=observe) r1 = foundation.electrical.elements.resistor(R = p1); [...] end [...] end
You do not have to assign all the parameters of member blocks to top-level parameters. If a member block parameter does not have a corresponding top-level parameter, the composite model uses the default value of this parameter, specified in the member component.
Caution on Using setup
to Parameterize Composite Components
You can establish the connection of a top-level parameter with a member component
parameter either in the components
declaration block,
or later, in the setup
section. Starting in R2019a,
using setup
is not recommended. If you have legacy code
using the setup
function, update it to use parameter
assignment in the components
block instead. For
example, this code is equivalent to the example above:
component MyCompositeModel [...] parameters p1 = {10, 'Ohm'}; [...] end components(ExternalAccess=observe) r1 = foundation.electrical.elements.resistor; ... end [...] function setup r1.R = p1; end [...] end
Note
In case of conflict, assignments in the setup
section override
those made in the declaration section.
Components are instantiated using default parameter values in the declaration section
before setup
is run. Therefore, if you make adjustments to the parameters
in the setup
section, use a subsequent setup
section
assignment to establish proper connection between the top-level parameter with a member
component parameter, as shown in the following example:
component RC nodes p = foundation.electrical.electrical; % :right n = foundation.electrical.electrical; % :left end parameters R = {1 , 'Ohm'}; % Resistance tc = {1 , 's'}; % RC time constant end parameters(ExternalAccess=observe) C = {1 , 'F'}; end components(ExternalAccess=observe) c1 = foundation.electrical.elements.capacitor(c=C); r1 = foundation.electrical.elements.resistor(R=R); end function setup C = tc/R; c1.c = C; % This assignment ensures correct operation end connections connect(c1.p, p); connect(c1.n, r1.p); connect(r1.n, n); end end