Control Linked Blocks Programmatically
Find and Modify Library Details of a Linked Block
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...'
LinkStatus: 'resolved'
The ReferenceBlock
parameter indicates that the Check ↵Static Range
block from the Simulink® library is the parent of the linked block CheckRange
in the model. To change the ReferenceBlock
to Check ↵Dynamic Range
, use the set_param
function:
set_param(linkedBlock,'ReferenceBlock',... ['simulink/Model Verification/Check ',newline,'Dynamic Range'])
Get the library details of the linked block CheckRange
in the model.
libinfo(linkedBlock)
ans = struct with fields:
Block: 'sldemo_fuelsys/fuel_rate_control/validate_sample_time/CheckRange'
Library: 'simulink'
ReferenceBlock: 'simulink/Model...'
LinkStatus: 'resolved'
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
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
and
StaticLinkStatus
parameters.
StaticLinkStatus
— Indicates the link status of a block without updating the linked blocks. This parameter is useful to query the status of linked blocks that are either active or outdated without changing or updating their current state. To get theStaticLinkStatus
, useget_param(gcb,'StaticLinkStatus')
.LinkStatus
— Updates the linked blocks and reflects the real-time status of a block. To get theLinkStatus
, useget_param(gcb,'LinkStatus')
.
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 the
|
inactive | The link to the library block is disabled. |
You can set the LinkStatus
to one of these values using the
set_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. |
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. |
Get and Set Link Status of Linked Blocks
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')
status_1 = 'none'
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')
status_2 = 'resolved'
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'.
status_3 = 'unresolved'
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')
status_4 = 'inactive'
Restore the link of Amplifier 4
.
set_param("LibrariesDemo/Amplifier 4",'LinkStatus','restore') status_4 = get_param("LibrariesDemo/Amplifier 4",'LinkStatus')
status_4 = 'resolved'
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')
status_5 = 'implicit'
Considerations When Getting Link Status
When you query the link status of a block using the
LinkStatus
parameter, any outdated block links are also resolved.When
get_param
is used in the callback code of a child block, it is recommended to use theStaticLinkStatus
parameter to query the link status, asStaticLinkStatus
does not resolve any outdated links.If you use the
get_param
function on a block that is inside a library link, Simulink® automatically resolves any necessary links. This process may involve loading parts of the library and executing any associated callback functions.