Main Content

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.

ActionCommand

Move the block name to the top of the block.

Sine Wave block with the block name myBlock on top of the block.

set_param(hb,NameLocation="top");

Move the block name to the bottom of the block.

Sine Wave block with the block name myBlock on bottom of the block.

set_param(hb,NameLocation="bottom");

Move the block name to the left side of the block.

Sine Wave block with the block name myBlock on the left side of the block.

set_param(hb,NameLocation="left");

Move the block name to the right side of the block.

Sine Wave block with the block name myBlock on 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.

ActionCommand

Hide the block name when the block is not selected regardless of whether the name is an automatic name.

Sine Wave block with the block name hidden.

set_param(hb,ShowName="off");

Always show the block name regardless of whether the name is an automatic name.

Sine Wave block with the block name showing.

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 ShowName or HideAutomaticName parameter is set to off.

A Sine Wave block is connected to a Gain block that is connected to a Scope block. The Gain block is selected. The block name of the Gain block is shown. The block names of the other two blocks are hidden.

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 ShowName or HideAutomaticName parameter is set to off.

A Sine Wave block is connected to a Gain block that is connected to a Scope block. The Gain block is selected. The block names of all three blocks are shown.

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 ShowName parameter to on for each block using the arrayfun function.

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 ShowName parameter to on for each block using the arrayfun function.

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.

ActionCommand

Get the label of a signal line.

get_param(h,Name)

Label an unlabeled signal line myLine.

A Constant block named myBlock1 connects to a Gain block named myBlock2. The signal line is labeled myLine.

set_param(h,Name="myLine");

Change a signal line label from myLine to speed.

A Constant block named myBlock1 connects to a Gain block named myBlock2. The signal line is labeled speed.

set_param(h,Name="speed");

Delete a signal line label.

A Constant block named myBlock1 connects to a Gain block named myBlock2. The signal line has no 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.

The Subsystem block.

ExampleCommand

Hide the port labels.

The Subsystem block showing no port labels.

set_param(h,ShowPortLabels="none")
  1. Show the port block names as the port labels.

    The Subsystem block showing the port labels In1, Out1, and Out2.

  2. Get the names of the port labels.

  3. Change the input port name to measured and the output port names to original and amplified.

    The Subsystem block showing the port labels measured, original, and amplified.

  1. To show the port block names as the port labels, use this command.

    set_param(h,ShowPortLabels="FromPortBlockName")
  2. Since the port labels are now the port block names, to get the port labels, get the port block names.

    Get the handles of all input and output port blocks at the root level of the subsystem.

    hPortBlocks = find_system(h,SearchDepth="1",regexp="on", ...
    blocktype="Inport|Outport");

    Use the block handles to get the block names.

    portLabels = string(get_param(hPortBlocks,"Name"))
  3. To change the port labels, change the port block names.

    Use the find_system function to get the handle of the input port block with the name In1 and the output port blocks with the names Out1 and Out2 at the root level of the subsystem.

    hIn1 = find_system(h,SearchDepth="1",Name="In1");
    hOut1 = find_system(h,SearchDepth="1",Name="Out1");
    hOut2 = find_system(h,SearchDepth="1",Name="Out2");

    Change the port labels by changing the port block names.

    set_param(hIn1,Name="measured")
    set_param(hOut1,Name="original")
    set_param(hOut2,Name="ampflified")
  1. Show the signal line labels as the port labels.

    The Subsystem block showing the port labels signal A, signal A, and signal B.

  2. Get the names of the port labels.

  3. Change the label of the ports connected to the non-amplified signal to original signal and the label of the ports connected to the amplified signal to amplified signal.

    The Subsystem block showing the port labels original signal, original signal, and amplified signal.

  1. To show the signal line labels as the port labels, use this command.

    set_param(h,ShowPortLabels="SignalName")
  2. Since the port labels are now the line labels, to get the port labels, get the signal line labels.

    Get the line handles.

    lines = get_param(h,"Lines");
    hLines = [lines.Handle];

    Use the line handles to get the line labels.

    portLabels = string(get_param(hLines,"Name"))
  3. To change the port labels, change the line labels.

    Use the find_system function to get the handles of all signal lines with the labels signal A and signal B at the root level of the subsystem.

    hSignalA = find_system(h,SearchDepth="1",FindAll="on", ...
    Name="signal A",Type="Line");
    hSignalB = find_system(h,SearchDepth="1",FindAll="on", ...
    Name="signal B",Type="Line");
    

    For each specified label, the function returns a vector containing the handles of the signal lines with the label and the handles of all branches of those signal lines. To change the port labels, change the name of the signal lines or any one of their branches.

    set_param(hSignalA(1),Name="original signal")
    set_param(hSignalB(1),Name="ampflified signal")

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.

The Subsystem block showing the port labels 1, 1, and 2.

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.

See Also

|

Topics