Configure Model Element Names and Labels Programmatically
You can get and set the text of signal line labels, block names, and subsystem port names programmatically. You can also change the location and visibility of block names. For information about how to change the text color and font, see Configure Model Style Elements Programmatically. To specify names or labels that are multiple lines or contain special characters, see Specify Paths That Contain Multiline Names and Special Characters.
The model, library, or subsystem containing the signal lines, blocks, or ports whose names you want to configure must be loaded. For information about how to load models, libraries, and subsystems, see Create, Load, Open, Save, and Close Models Programmatically.
Configure Block Names
The Simulink® Editor names blocks when you add them to a model. The first occurrence of the block is the library block name, for example, Gain. The next occurrence is the block name with a number appended. Each new block increments the number, for example, Gain1, Gain2, and so on. These names are called automatic names. You can use automatic names, or you can customize block names.
To programmatically get the name of a block with the handle hb
, use
this command. For information about how to get block handles, see Get Handles and Paths.
get_param(hb,"Name")
To programmatically change the name of a block, the position of the name relative to a
block, or the visibility of the name, use the set_param
function.
To change the name of a block with the handle hb
, use this command.
Replace newName
with the new name you want to give the block.
set_param(hb,Name="newName");
The table shows how to change the position of a block with the handle
hb
.
Action | Command |
---|---|
Move the block name to the top of the block. |
set_param(hb,NameLocation="top"); |
Move the block name to the bottom of the block. |
set_param(hb,NameLocation="bottom"); |
Move the block name to the left side of the block. |
set_param(hb,NameLocation="left"); |
Move the block name to the right side of the block. |
set_param(hb,NameLocation="right"); |
The next table shows how to change the visibility of a block with the handle
hb
. By default, an automatic name is shown when the block is selected
and hidden when the block is not selected. If you change the name, then by default, the name
is always shown.
Action | Command |
---|---|
Hide the block name when the block is not selected regardless of whether the name is an automatic name. |
set_param(hb,ShowName="off"); |
Always show the block name regardless of whether the name is an automatic name. |
set_param(hb,ShowName="on",HideAutomaticName="off") |
Change the model settings to always show automatic names. Changing this
setting does not affect blocks whose names have been changed from the default or
whose |
set_param(hm,HideAutomaticNames="off"); |
Change the model settings to only show the automatic names of selected
blocks. Changing this setting does not affect blocks whose names have been changed
from the default or whose |
set_param(hm,HideAutomaticNames="on"); |
Hide the names of all blocks in a model regardless of whether the names are automatic names. Exclude referenced models, libraries, and subsystems from the operation. | Get the handles of all blocks in the model. hb = find_system(hm,Type="block") Set
the arrayfun(@(h) set_param(h,ShowName="off"),hb) |
Show the names of all blocks in a model regardless of whether the names are automatic names. Exclude referenced models, libraries, and subsystems from the operation. | Get the handles of all blocks in the model. hb = find_system(hm,Type="block") Set
the arrayfun(@(h) set_param(h,ShowName="on",HideAutomaticName="off"),hb) |
Configure Signal Line Labels
Use the get_param
and set_param
functions to configure signal line labels. The table shows examples
for an unlabeled signal line with the handle h
. For information about how
to get line handles, see Get Handles and Paths.
Action | Command |
---|---|
Get the label of a signal line. |
get_param(h,Name) |
Label an unlabeled signal line |
set_param(h,Name="myLine"); |
Change a signal line label from |
set_param(h,Name="speed"); |
Delete a signal line label. |
set_param(h,Name=""); |
Configure Subsystem Port Labels
By default, the port labels on the subsystem are the port numbers. If you change the name of any of the corresponding port blocks inside the subsystem, the port label is the block name instead of the port number. You can configure a subsystem to hide the port labels, to show the names of the corresponding port blocks for all ports, or to show the names of the signal lines connected to the ports inside the subsystem. The option you choose determines whether and how you can show custom port labels.
Use the get_param
and set_param
functions to configure port labels. The table shows examples for a
Subsystem block with the handle h
. For information about
how to get block handles, see Get Handles and Paths.
The block has one input port and two output ports. Inside the subsystem, the ports are
represented by the In1 block, the Out1 block, and the
Out2 block. The In1 block connects to the
Out1 block and to a Gain block with a signal line labeled
signal A
. The Gain block connects to Out2
block with a signal line labeled signal B
.
Example | Command |
---|---|
Hide the port labels. |
set_param(h,ShowPortLabels="none") |
|
|
|
|
Return the port label configuration to the default. If the port block names are the default names, show the port numbers as the port labels. If the name of a port block has been changed from the default, show the port block name as the corresponding port label. |
set_param(h,ShowPortLabels="FromPortIcon") |
Note
When you want to rename multiple ports and are trying to establish which name to assign to which port, be aware that signal lines and signal line branches have different handles. Consider this command.
get_param(hBlock,"LineHandles")
If hBlock
is the handle of the block to whose output port a
branched signal line connects, the get_param
function returns the
handle of the whole signal line, including all branches. If hBlock
is
the handle of the block to whose input port that same branched signal line connects, the
function returns the handle of the branch, not the whole signal line. Do not try to
establish that two blocks connect to the same branched signal line using line handles.
Compare signal line labels instead.