Control Linked Blocks Programmatically - MATLAB & Simulink (original) (raw)

Main Content

Find and Modify Library Details of a Linked Block

Use the libinfo function to get information about the library blocks referenced in a model. libinfo provides information about the path of the linked block in a model, its source library name, the path to the library block in its source library, and the link status.

Load the model and get library details of a specific block referenced in the model.

load_system("sldemo_fuelsys"); linkedBlock = "sldemo_fuelsys/fuel_rate_control/validate_sample_time/CheckRange"; libinfo(linkedBlock)

ans = struct with fields: Block: 'sldemo_fuelsys/fuel_rate_control/validate_sample_time/CheckRange' Library: 'simulink' ReferenceBlock: 'simulink/Model↵Verification/Check ↵Static Range' LinkStatus: 'resolved'

The ReferenceBlock parameter indicates that the Check ↵Static Range from the Simulink® library is the parent library block of CheckRange block. To change the ReferenceBlock to Check ↵Dynamic Range, use the set_param function:

set_param(linkedBlock,'ReferenceBlock',... 'simulink/Model Verification/Check Dynamic Range'); libinfo(linkedBlock).ReferenceBlock

ans = 'simulink/Model Verification/Check Dynamic Range'

Note: It is not recommended to change the properties of a referenced block by using the set_param function in the mask initialization code or the callback code of the same block. For such modeling patterns, you can either use Variant blocks or set the ReferenceBlock parameter in the mask initialization code or the callback code of the parent block of the reference block.

Lock Linked Blocks

You can lock the links between a parent library block and its linked blocks. When you lock the links to a library, you can no longer edit or disable the links from a library block instance used in a model. This behavior prevents unintentional modifications and enhances the protection of your library blocks.

Use the LockLinksToLibrary parameter to lock or unlock a linked block in a library from the command line.

To lock the links, use:

set_param("MyLibraryName","LockLinksToLibrary","on");

To unlock the links, use:

set_param("MyLibraryName","LockLinksToLibrary","off");

Link status provides information about the connection between a referenced block in a model and its corresponding block in a library. You can get the link status of a block in a model by using the LinkStatus andStaticLinkStatus parameters.

LinkStatus can have one of these values:

Get LinkStatus Value Description
none The block is not a linked block.
resolved The link to the library block is active and valid.
unresolved The link to the library block cannot be resolved. For example, the library block is missing or the path is incorrect.
implicit The block resides within a library block and is not directly linked to a library block itself. For example, if a library subsystem contains a Gain block and this subsystem has a linked block A in a model, when you select the Gain block within the block A in the model, then theget_param(gcb,'LinkStatus') function returns the LinkStatus asimplicit.
inactive The link to the library block is disabled.

You can set the LinkStatus to one of these values using theset_param function. For example, to break a link to the library, use set_param(gcb,'LinkStatus','none').

Set LinkStatus Value Description
none Breaks the link to the library block.
breakWithoutHierarchy Breaks the link without affecting the nested parent hierarchy of the link.
inactive Disables the link to the library block.
restore Restores an inactive link to the library block and replaces the version of the linked block in the model with the version in the library. restore discards any changes made to the local copy of the linked block in the model.
propagate Pushes the changes made to an inactive linked block in a model to its corresponding library block and re-establishes its link. Pushing a link replaces the version of the block in the library with the version in the model.
restoreHierarchy Restores all disabled links in the link hierarchy to their corresponding library blocks and replaces the version of the linked blocks in the model with the version in the library. For more information on hierarchy mode, see Pushing or Restoring Link Hierarchies.
propagateHierarchy Pushes all the changes made to the linked blocks in the link hierarchy back to their respective library blocks. Pushing the links replaces the version of the blocks in the library with the version in the model. For more information on hierarchy mode, see Pushing or Restoring Link Hierarchies.

A model LibrariesDemo has four Subsystem blocks.

load_system("LibrariesDemo.slx")

Get the link status of Subsystem block Amplifier 1. Amplifier 1 is not a linked block.

status_1 = get_param("LibrariesDemo/Amplifier 1",'LinkStatus')

Get the link status of Subsystem block Amplifier 2. Amplifier 2 is a linked block from a custom library AmplifierLibrary with 'resolved' link status.

status_2 = get_param("LibrariesDemo/Amplifier 2",'LinkStatus')

Get the link status of Subsystem block Amplifier 3. Amplifier 3 is a linked block from a custom library LibA with 'unresolved' link status as the library is not located at the specified path.

status_3 = get_param("LibrariesDemo/Amplifier 3",'LinkStatus')

Warning: Cannot find library called 'LibA'.

Get the link status of Subsystem block Amplifier 4. Amplifier 4 is a linked block from a custom library AmplifierLibrary with 'inactive' link status.

status_4 = get_param("LibrariesDemo/Amplifier 4",'LinkStatus')

Restore the link of Amplifier 4.

set_param("LibrariesDemo/Amplifier 4",'LinkStatus','restore') status_4 = get_param("LibrariesDemo/Amplifier 4",'LinkStatus')

Get the link status of Gain block in Subsystem block Amplifier 2. The link status of Gain block is 'implicit' as the Gain block resides within a library block and is not directly linked to a library block itself.

status_5 = get_param("LibrariesDemo/Amplifier 2/Gain",'LinkStatus')

See Also

Functions

Tools

Topics