Referencing Configuration Sets

Overview of Configuration References

By default, a configuration set is stored within an individual model, which allows it to be used only by that model. Alternatively, a configuration set can stored independently of any model, which allows it to be used by any or all models.

A configuration set that exists outside any model is called a freestanding configuration set. Each model that uses a freestanding configuration set does so by defining a configuration reference that points to the configuration set. The result is the same as if the referenced configuration set were stored within the model.

You can use configuration references and freestanding configuration sets to:

The next figure shows one way to use configuration references. Each of the four models represented in the Model Dependency Viewer specifies the configuration reference named my_reference as its active configuration set, and my_reference points to a freestanding configuration set named Configuration. The parameter values in Configuration therefore apply to all four models, and any change to any parameter in Configuration applies to all four models.

To use a configuration reference to link a freestanding configuration set to a model, you:

  1. Create or obtain a configuration set and store it in the base workspace as the value of a MATLAB variable.

  2. Create a configuration reference that specifies the relevant MATLAB variable.

  3. Attach the configuration reference to the model just as you would attach a configuration set.

  4. Activate the reference just as you would activate a configuration set stored within the model.

  5. Access, set, and change configuration set parameters and the MATLAB variable as needed.

A configuration reference is implemented as an object of type Simulink.ConfigSetRef, and a configuration set is an object of type Simulink.ConfigSet The two classes are similar in many ways. Wherever the same operation is applicable to both, the relevant functions and methods are overloaded to work with either class. For example, you can attach or activate a configuration set or a configuration reference using the same GUI operations and API syntax.

You cannot nest configuration references: only one level of indirection is available. You can obtain configuration parameter values by operating on a configuration reference just as if it were the configuration set that it references. See Getting Values from a Referenced Configuration Set for details. General information about configuration sets appears in Configuration Sets.

Creating a Freestanding Configuration Set

All freestanding configuration sets are stored in the base workspace as the values of base workspace variables. Although you can store a configuration set in a model and point to it with a base workspace variable, such a configuration set would not be freestanding; trying to use it in a configuration reference would cause an error. You can store a freestanding configuration set in the base workspace in these ways:

You can store any number of configuration sets in the base workspace, assigning each to a different variable. You can use any technique to manipulate a freestanding configuration set and its parameter values that you could use with a configuration set stored directly in a model.

Creating and Populating a New Configuration Set

You can create a new configuration set in the base workspace using any of the techniques described in Creating Configuration Sets or as follows:

cset = Simulink.ConfigSet

where cset is a new or existing base workspace variable. The new configuration set initially has default parameter values, copied from the default configuration set.

Copying a Configuration Set Stored in a Model

You can copy an existing configuration set to the base workspace using drag and drop operations described in Copying, Deleting, and Moving Configuration Sets, and assign the set to a MATLAB variable. For example:

cset = copy (getActiveConfigSet(mdl))
cset = copy (getConfigSet(mdl, ConfigSetName))

where mdl is any open model, and ConfigSetName is the name of any configuration set attached to the model. The first example obtains the currently active configuration set. The second example obtains a configuration set by specifying the name under which it appears in the Model Explorer.

Be sure to copy any configuration set obtained from an existing model, as shown in the examples. Otherwise, cset will refer to the existing configuration set stored in the model, rather than a new freestanding configuration set in the base workspace, and any use of a configuration references that links to cset will cause an error.

Reading a Configuration Set from a MAT-File

To use a freestanding configuration set across multiple MATLAB sessions, you can save it into a MAT-file. To create the MAT-file, you first copy the configuration set to a base workspace variable, as previously described, then save the variable to the MAT-file:

save (workdir/ConfigSetName.mat, cset)

where workdir is a working directory, ConfigSetName.mat is the name of the MAT-file, and cset is a base workspace variable whose value is the configuration set to be saved. When you later reopen your model, you can reload the configuration set into the variable:

load (workdir/ConfigSetName.mat)

To execute code that reads configuration sets from MAT-files, you can use the pre-load function of a top model, the MATLAB startup script, and various other techniques, including entering the load statement(s) interactively. Any technique that executes the necessary code will work.

Creating and Attaching a Configuration Reference

Once you have stored a configuration set in the base workspace, as described in Creating a Freestanding Configuration Set, you can link to that configuration set from a configuration reference, and attach the reference to a model. The model then has the same configuration parameters that it would if the referenced configuration set were stored directly in the model. You can attach any number of configuration references to a model. Each must have a unique name.

GUI Techniques

To create a configuration reference using the GUI:

  1. In the Model Explorer, select the model to which the configuration reference will be attached.

  2. Click the Add Reference tool or choose Add > Configuration Reference.

    A new configuration reference attaches to the selected model. The default name of the new reference is Reference, with a digit appended if necessary to prevent name conflict. The name of the configuration reference appears in the Model Hierarchy pane under the Model Workspace icon, below the names of any configuration sets.

  3. Select the new configuration reference in the Model Hierarchy pane, or right-click the configuration reference and choose Open from the context menu.

    The Configuration Reference dialog appears in the Dialog pane or a separate window.

  4. Change the default Name if desired. This name exists for human readability, and does not affect the configuration reference functionally.

  5. Specify the Referenced configuration set to be the base workspace variable whose value is the freestanding configuration set that you want to reference. Be careful not to specify the name of a configuration reference. Configuration references cannot be nested, and an error will result.

  6. Click OK or Apply.

    The Is Resolved field in the dialog changes to yes.

If you do not specify a valid Referenced configuration, a warning is posted. Any attempt to use a configuration reference that lacks a valid Referenced configuration generates an error. The API equivalent of Referenced configuration is WSVarName. You can later use the GUI or API to correct the specification or provide a configuration set with the correct name. See Unresolved Configuration References for more information.

API Techniques

To create and populate a configuration reference using the API:

  1. Create the reference:

    cref = Simulink.ConfigSetRef
  2. Change the default name if desired:

    cref.Name = 'ConfigSetRefName'
  3. Specify the referenced configuration set:

    cref.WSVarName = 'cset'

    Be careful not to specify the name of a configuration reference. Configuration references cannot be nested, and an error will result.

  4. Attach the reference to a model:

    attachConfigSet(mdl, cref, true)

    The third argument is optional and authorizes renaming if needed to avoid a name conflict.

Any attempt to use a configuration reference that does not specify a valid WSVarName generates an error. The GUI equivalent of WSVarName is Referenced configuration. You can later use the API or GUI to correct the reference or provide a configuration set that has the correct name. See Unresolved Configuration References for more information.

Obtaining a Configuration Reference Handle

Most functions and methods that operate on a configuration reference take a handle to the reference. If you have created a configuration reference programmatically, with a statement like

cref = Simulink.ConfigSetRef

the variable cref contains a handle to the reference. If you do not already have a handle, you can obtain one by executing:

cref = getConfigSet(mdl, 'ConfigSetRefName')

where ConfigSetRefName is the name of the configuration reference as it appears in the Model Explorer, e.g., Reference. This is the name you specified by setting the Name field in the Configuration Reference dialog or executing

cref.Name = 'ConfigSetRefName'

The technique for obtaining a configuration reference handle is the same as you would use to obtain a configuration set handle. Wherever the same operation applies to both configuration sets and configuration references, applicable functions and methods are overloaded to perform correctly with either class.

Attaching a Configuration Reference to Additional Models

After you have created a configuration reference and attached it to a model, you can attach copies of the reference to any number of additional models. To create and attach a copy of a configuration reference, you can use any GUI or API technique that you could use to copy and attach a configuration set, such as dragging, pasting, or a function like attachConfigSetCopy. See Copying, Deleting, and Moving Configuration Sets and Configuration Set API.

Models do not share configuration reference objects. Each model has its own copy of any configuration reference attached to it, just as each has its own copy of any attached configuration set. Configuration references in different models establish configuration set sharing by specifying the same base workspace variable, which links the various models to the same freestanding configuration set.

If you use the GUI, attaching an existing configuration reference to an additional model automatically attaches a copy, as distinct from a handle to the original. If necessary to prevent name conflict, the GUI will add or increment a digit at the end of the copied reference's name. If you use the API, be sure to explicitly copy the configuration reference before attaching it, with statements like:

cref = copy (getConfigSet(mdl, ConfigSetName))
attachConfigSet (cref, mdl, true)

If you omit the copy operation, cref will be a handle to the original configuration reference, rather than a copy of the reference, and any attempt to use cref will cause an error. If you omit the argument true to attachConfigSet, the operation will fail if it would cause a name conflict.

The following example shows code for obtaining a freestanding configuration set and attaching references to it to two models. After the code executes, one of the models contains both an internal configuration set and a configuration reference that points to a freestanding copy of that configuration set. If the internal copy is superfluous, it can be removed with detachConfigSet, as shown in the last line of the example.

% Get copy of original config set as 
% a variable in the base workspace
open_system('mdl1')
% Get handle to local cset
cset = getConfigSet('mdl1', 'Configuration')
% Create freestanding copy; original remains in model
cset1 = copy(cset)
% In the original model, create a configuration 
% reference to the cset copy
cref1 = Simulink.ConfigSetRef
cref1.WSVarName = 'cset1'
% Rename if name conflict occurs
attachConfigSet('mdl1', cref1, true)

% In a second model, create a configuration 
% reference to the same cset 
open_system('mdl2')
% Rename if name conflict occurs
attachConfigSetCopy('mdl2', cref1, true)
% Delete original cset from first model
detachConfigSet('mdl1', 'Configuration')

Changing a Configuration Reference

You can change an existing configuration reference as needed by reopening its Configuration Reference dialog and changing its Name or Referenced configuration. Similarly, you can use the API on an existing configuration reference to change its Name or WSVarName. If you refer to a configuration set that does not yet exist, no error occurs, but the configuration reference is unusable. The configuration reference becomes usable as soon as the configuration set exists.

Activating a Configuration Reference

Once you have created a configuration reference and attached it to a model, you can activate it using any technique that would activate a configuration set stored in the model, such as:

When a configuration reference is active, the Is Active field of the Configuration Reference dialog is yes, and the Model Explorer shows the name of the reference suffixed with (Active).

The freestanding configuration set to which the active reference points now provides the configuration parameters for the model containing the reference.

Unresolved Configuration References

When a configuration reference does not specify a valid configuration set, the configuration reference is unresolved, and the Is Resolved field of its Configuration Reference dialog has the value no. If you activate an unresolved configuration reference, no warning or error occurs. However, an unresolved configuration reference that is Active provides no configuration parameter values to the model. Therefore:

Creating and Attaching a Configuration Reference describes techniques for resolving configuration references.

Getting Values from a Referenced Configuration Set

You can use get_param on a configuration reference to obtain parameter values from the linked configuration set, just as if the reference object were the configuration set itself. The Simulink software retrieves the referenced configuration set and performs the indicated get_param on it.

For example, if configuration reference cref links to configuration set cset, the following operations give identical results:

get_param (cset, 'StopTime')
get_param (cref, 'StopTime')

Changing Values in a Referenced Configuration Set

You cannot change a configuration set in any way, including changing configuration parameter values, by operating on a configuration reference. Thus, with cref and cset as above, if you executed:

set_param (cset, 'StopTime', 300)
set_param (cref, 'StopTime', 300)          % ILLEGAL

the first operation would succeed, but the second would cause an error. Instead, you must obtain the configuration set itself and change it directly, using the GUI or the API.

GUI Techniques

To obtain a referenced configuration set using the GUI:

  1. Select the configuration reference in the Model Hierarchy pane, or right-click the configuration reference and choose Open from the context menu.

    The Configuration Reference dialog appears in the Dialog pane or a separate window.

  2. Click Open to the right of the Referenced configuration field.

    The Configuration Parameters dialog box opens on the configuration set specified by Referenced configuration. You can now change and apply or save parameter values as you would for any configuration set.

API Techniques

To obtain a referenced configuration set using the API:

  1. Obtain a handle to the configuration reference, as described in Obtaining a Configuration Reference Handle.

  2. Obtain the configuration set cset from the configuration reference cref:

    cset = cref.getRefConfigSet

    You can now use set_param on cset to change parameter values. Example:

    set_param (cset, 'StopTime', 300)

    If you want to change parameter values through the GUI, execute:

    cset.openDialog

    The Configuration Parameters dialog box opens on the specified configuration set.

Replacing a Referenced Configuration Set

You can completely replace the base workspace variable and configuration set that a configuration reference uses. However, the pointer from the configuration reference to the configuration set becomes stale. You must then execute:

cref.refresh

where cref is the configuration reference. If you do not execute refresh, the configuration reference will continue to use the previous instance of the base workspace variable and its configuration set. This example illustrates the problem.

% Create a new configuration set
cset1=Simulink.ConfigSet;
% Set a non-default stop time
set_param (cset1, 'StopTime', 500)
% Create a new config reference
cref1=Simulink.ConfigSetRef;
% Resolve the config ref to the set
cref1.WsVarName='cset1';
% Attach the config ref to a model
attachConfigSet('mdl1', cref1, true)
% Replace config set on base workspace
cset1=Simulink.ConfigSet;
% Call to refresh is commented out!
% cset1.refresh;
% Set a different stop time
set_param (cset, 'StopTime', 75)

If you simulate the model, it will stop at 500, not at 75. Calling cset1.refresh where shown prevents the problem.

Building Models and Generating Code

The Real-Time Workshop® pane of the Configuration Parameters dialog contains a Build button. Its availability differs depending on whether the configuration set displayed by the dialog is stored in a model or is a freestanding configuration set.

To provide the same capabilities whether a configuration set is stored in a model or is freestanding, the Configuration Reference dialog contains a Build button. This button has the same effect as its equivalent in the Configuration Parameters dialog, and operates on the model that contains the configuration reference.

Configuration Reference Limitations

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS