Subsystem Reference

Subsystem reference allows you to save the contents of a subsystem in a separate SLX file and reference it using a Subsystem Reference block. You can create multiple instances referencing the same subsystem file. When you edit any instance of a referenced subsystem, the changes are saved in the separate SLX file in which the subsystem is stored and all the referenced instances of that file are synchronized.

When you save a subsystem to a separate file you can reuse it multiple times by using Subsystem Reference blocks referencing the same subsystem file.

You can identify a Subsystem Reference block by the triangles in the opposite corners of the block icon.

A referenced subsystem supports all the semantics of a regular subsystem. A referenced subsystem adapts itself to the context of the parent model and has identical execution behavior when compared to a nonreferenced subsystem.

Create a Subsystem file

A subsystem file stores the content of a subsystem in an SLX file.

A subsystem file:

  • Cannot be simulated.

  • Does not have a configuration set.

  • Does not have a model workspace.

  • Does not have code generation capability.

To create a subsystem file:

  1. Open Simulink®.

  2. On the Start Page, in the Simulink product group, click Blank Subsystem.

    Alternatively, in the Simulink toolstrip, on the Simulation tab, select New and click Blank Subsystem. A Subsystem window opens.

  3. Add content and click Save.

  4. Specify a file name in the Save As dialog box. The file name must be a valid MATLAB name.

    This creates a new subsystem file at the location specified.

To create a subsystem file programmatically, use the command:

new_system(subsystemfilename,'subsystem')
Once you create the subsystem file programmatically, it appears in the MATLAB File Browser as:

For more information on controlling subsystem files programmatically, see Control Referenced Subsystem Programmatically.

Reference a Subsystem File in a Model

  1. Open a model in which you want to reference a subsystem file.

  2. Add a Subsystem Reference block using the Simulink Library Browser or the Quick Block Insert menu. An empty Subsystem Reference block is added to the Simulink canvas.

  3. Double-click the Subsystem Reference block.

  4. In the Block Parameter dialog box, specify the name of the subsystem file or click Browse to navigate to it.

  5. Click OK to save the changes.

Convert an Existing Subsystem to a Referenced Subsystem

You can convert an existing Subsystem block to a Subsystem Reference block.

Consider a model with two Subsystem blocks as shown.

In this model, you have two Subsystem blocks – a Controller subsystem and a Plant subsystem. To convert the Controller Subsystem block to a referenced subsystem:

  1. Select the Controller Subsystem block and on the Subsystem Block tab, select Convert and click Convert to Subsystem Reference.

  2. Specify a name for the subsystem component in the Subsystem file name field and click Convert. The name must be a valid MATLAB name. The conversion creates a subsystem file in the current directory. To create the file in a specific location, click Browse and navigate to the save location.

    The Subsystem block changes into a Subsystem Reference block with the name of the subsystem file displayed at the top of the block icon.

Tip

When you convert a linked block to a referenced subsystem, do so in the parent library block of that linked block. Once you convert a parent library block to a referenced subsystem, all its linked block instances are also converted to referenced subsystems.

You cannot convert a subsystem to a referenced subsystem when the subsystem:

  • Has a test harness associated to it.

  • Has no read/write permissions.

  • Is a library block that has a self-modifiable mask.

Edit and Save Referenced Subsystem

In a model containing multiple instances of a referenced subsystem, you can edit any instance and upon saving the changes the updates propagate to all the referenced instances. When you actively edit an instance of a referenced subsystem, all the other referenced instances are locked and are unavailable for edit.

A badge is shown at the bottom left corner of an open subsystem file to denote the availability of the file for edit. The badge shows two states:

  • indicates that the subsystem file is available for edit. Right-click the badge to see all the active instances of that subsystem and to navigate to each of them.

  • indicates that the subsystem file is unavailable for edit, as another instance is being actively edited. Right-click the badge to open the instance being edited.

Once you have edited an instance in a model, you can save the changes from the Save option available in the Simulation tab of the model window. The Save All drop-down gives you two options to save your changes:

  • Save All – To save all the updates in the current model.

  • Save Specific Referenced File – To save a specific subsystem file when you have made changes to multiple subsystem files.

Note

If you edit any instance of a referenced subsystem in a model, save the changes before updating or simulating the model. Unsaved changes in referenced instances cause error during simulation.

Add a Model Mask for Subsystem Reference

You can mask a subsystem file using a model mask. When you create a model mask for a subsystem file, all the referenced instances share the same model mask.

To mask a subsystem file:

  1. Open the subsystem file to be masked.

  2. In the Simulink toolstrip, on the Modeling tab, Component gallery, click Create Model Mask. Alternatively, right-click anywhere on the canvas and select Mask and click Create Model Mask. The Mask Editor dialog opens.

  3. Add mask parameters and click OK.

Note

You cannot directly mask a Subsystem Reference block. To mask a Subsystem Reference block, right-click the block and select Mask > Open file and Create Model Mask . This action opens the subsystem file being referenced and creates a model mask on it.

For more information on creating and editing model masks, see Introduction to Model Mask.

Subsystem Reference Compatibility with Previous Versions

When you export a model containing referenced subsystems to a version prior to R2019b, all the Subsystem Reference blocks are converted to independent Subsystem blocks.

Subsystem files cannot be exported to a version prior to R2019b. For information on how to export a simulink model to a previous version, see Export a Model to a Previous Simulink Version.

Control Referenced Subsystem Programmatically

You can create a referenced subsystem, find available referenced subsystems in a model, change the referenced subsystem file in a block, and check the block diagram type of an SLX file using a command-line interface.

Create a Referenced Subsystem

You can create a referenced subsystem using the new_system command:

new_system(subsystemfilename,'SubSystem')

Find Subsystem Reference in a Model

You can find if a model contains a referenced subsystem using the find_system command:

find_system(bdroot, 'RegExp','on','BlockType','SubSystem','ReferencedSubsystem', '.')

This command returns a list of Subsystem Reference blocks in the model. By default, find_system lists all the child blocks inside a subsystem reference instance.

If you do not want find_system to look inside a referenced subsystem, use find_system with LookInsideSubsystemReference set to off. By default, LookInsideSubsystemReference is set to on.

Change the Referenced File for a Subsystem

You can change the subsystem file being referenced in a Subsystem Reference block through command-line interface using the set_param command:

set_param(gcb, 'ReferencedSubsystem', '<subsystemfilename>')
This command changes the file being currently referenced by the Subsystem Reference block and replaces it with the new subsystem file you specify.

Check if the SLX File is a Subsystem Block Diagram Type

You can check if an SLX file is a subsystem block diagram type that can be placed in a Subsystem Reference block using any of these commands:

bdIsSubsystem(bdname)
This command returns logical 1 if bdname.slx is a Subsystem block diagram type and logical 0 if it is not. When using this command, make sure that bdname.slx is loaded.

get_param(bdname,'BlockDiagramType')

This command returns Subsystem if bdname.slx is a Subsystem block diagram type. When using this command, make sure that bdname.slx is loaded.

Simulink.MDLInfo(bdname)

This command gives the entire model information where the BlockDiagramType property is shown as Subsystem if bdname.slx is a Subsystem block diagram type.

Best Practices

While using a referenced subsystem in a model:

  • To mask a referenced subsystem, use a model mask.

  • Do not reference a parent subsystem because it creates a reference loop.

  • Only the subsystem file type can be referenced by a Subsystem Reference block.

Related Topics