Main Content

Automate Model Configuration by Using a Script

If you want to use the same configuration setup for many models, you can write a script to programmatically configure each model in the same way. You can use the script to archive and compare the configuration settings that your models use.

This example shows three different ways to programmatically set up your model's configuration:

  • Edit the model's existing active configuration set

  • Create and edit a new configuration set in the model

  • Create a configuration reference that points to a freestanding configuration set

For this example, use the model sldemo_mdlref_depgraph.

model = 'sldemo_mdlref_depgraph';
open_system(model)

Edit the Active Configuration Set

To manipulate a configuration set that is associated with a model, use the ConfigSet object that represents the configuration set. For this example, use the configuration that is active for the model.

activeConfigObj = getActiveConfigSet(model);
get_param(activeConfigObj,'Name')
ans = 
'Configuration'

The active configuration for the model is Configuration. To see the current values of parameters in the configuration, use the get_param function and the ConfigSet object.

get_param(activeConfigObj,'StopTime')
ans = 
'2*24*60*60'

Rename the configuration set to UpdatedConfig.

set_param(activeConfigObj,'Name','UpdatedConfig');

For this example, set a stop time of 200 and change the solver type to a variable-step solver.

set_param(activeConfigObj,'StopTime','200');
set_param(activeConfigObj,'SolverType','Variable-step');

Create and Activate a Configuration Set

When you want to change the model's configuration and preserve the original parameter values of its active configuration, create and activate a new configuration set in the model. To create another configuration set, copy an existing configuration set and attach the copy to the model. To avoid naming conflicts when you attach the copy, either rename the copy before attaching it or set allowRename, the optional third argument of attachConfigSet, to true.

For this example, copy the active configuration set. Rename the copy to ConfigCopy and attach it to the model.

newConfigObj = copy(activeConfigObj);
set_param(newConfigObj,'Name','ConfigCopy');
attachConfigSet(model, newConfigObj);

When you attach a configuration set to a model, it is inactive. You can manipulate inactive configurations in the same way that you manipulate the active configuration set. To use the new configuration, activate it for the model.

set_param(newConfigObj,'SolverType','Fixed-step');

setActiveConfigSet(model,'ConfigCopy');
activeConfigSet = getActiveConfigSet(model);
get_param(activeConfigObj,'Name')
ans = 
'UpdatedConfig'

Now, ConfigCopy is the active configuration set.

Set Up a Configuration Reference

If you want to store the configuration set outside of your model, create a script that sets up a configuration reference in the model. The reference is stored in the model and it points to a freestanding configuration set, which is stored in either a Simulink data dictionary or in the base workspace. Use a freestanding configuration set and configuration references to share one configuration with multiple models. You can also use a freestanding configuration set when you want to edit the configuration without changing the model file.

For this example, configure the model vdp to use a configuration reference. First, create a freestanding configuration set in the base workspace by copying the model's active configuration set. The freestanding configuration is a ConfigSet object represented by the variable freeConfigSet. You can skip this step if you want to reference an existing freestanding configuration set.

model = 'vdp';
open_system(model)

freeConfigSet = copy(getActiveConfigSet(model));

Create a configuration reference. To point the reference to your freestanding configuration, set the SourceName property to freeConfigSet, the variable that represents your configuration. The new reference is a ConfigSetRef object represented by the variable configRef. Name the reference vdpConfigRef.

configRef = Simulink.ConfigSetRef;
set_param(configRef,'SourceName','freeConfigSet')
set_param(configRef,'Name','VdpConfigRef')

Attach the configuration reference to the model vdp by using the ConfigSetRef object. You can attach the reference to only one model. To use the configuration reference in the model, activate it.

attachConfigSet('vdp',configRef);
setActiveConfigSet('vdp','VdpConfigRef');

Now, when you change the configuration set that the object freeConfigSet represents, the changes apply to the model.

You can obtain parameter values in a configuration reference by using get_param. However, you cannot change parameter values directly in the configuration reference. To change the values, you must use the ConfigSet object that represents the referenced freestanding configuration set. Get the freestanding configuration set from a configuration reference by using the getRefConfigSet method.

referencedConfigObj = getRefConfigSet(configRef);

Now, referencedConfigObj represents the same freestanding configuration set that your models reference. freeConfigSet represents that configuration set as well. Use the configuration set object to change parameter values in the referenced configuration set. These changes apply to each model that references the configuration.

set_param(referencedConfigObj,'SignalLogging','off');
set_param(referencedConfigObj,'StartTime','10');

Related Topics