Main Content

add_block

R2026b

Add block to model

Description

h = add_block(source,dest) adds a copy of the specified source block to the specified destination. The new block is added at the same position in the block diagram as the source block appears in its parent block diagram.

The destination model must be loaded.

example

h = add_block(___,Name=Value) specifies additional options using one or more Name=Value pair arguments.

example

Examples

collapse all

Suppose you have a model named myModel. Add a Circular Gauge block from the Customizable Blocks library to the model. Name the new block myBlock. Ensure the name is unique. For example, if a block with the name myBlock already exists, name the block myBlock1 instead.

Look up the path of the library block. Start by opening the library that contains the block.

You can do so using the quick insert menu. Open the quick insert menu by double-clicking the model canvas. Then, enter the block name. Use your arrow keys to navigate to the desired search result. In the details pane on the right, click the name of the library.

Quick insert menu with "circular gauge" entered in the search bar, showing the details pane for the Circular Gauge block

In the library that opens, select the Circular Gauge block.

Get the block path by entering this command in the MATLAB® Command window.

libBlockPath = gcb
libBlockPath =
    'simulink_hmi_customizable_blocks/Circular Gauge'

Add the block to myModel with the name myBlock. To ensure the name is unique, set MakeNameUnique to "on".

add_block(libBlockPath,"myModel/myBlock,"MakeNameUnique="on")

Suppose you have a model named myModel containing a block named myBlock.

Create a copy of the block. Name the copy myBlock1.

add_block("myModel/myBlock","myModel/myBlock1");

Suppose you have a model named myModel.

Add a Constant block to the model with a value of 0.1. Name the new block myBlock and ensure the name is unique.

add_block("simulink/Sources/Constant","myModel/myBlock",MakeNameUnique="on",Value="0.1");

How you add a block for a port depends on your goal. You can add a block for a new or existing port. You can also duplicate a block that corresponds with an input port.

Add Blocks for New Ports

Create and open a model named PortBlockCreation.

mdl = "PortBlockCreation";
new_system(mdl);
open_system(mdl);

To create ports identified by port number, add In1 and Out1 blocks. By default, adding an In1 or Out1 block creates a port.

add_block("simulink/Ports & Subsystems/In1",...
    "PortBlockCreation/In1")
add_block("simulink/Ports & Subsystems/Out1",...
    "PortBlockCreation/Out1")

To create ports identified by port name, add In Bus Element and Out Bus Element blocks. By default, new In Bus Element and Out Bus Element blocks correspond with ports named InBus and OutBus, respectively. These ports also have a port number. Optionally, specify a custom name for the port with the PortName block parameter.

add_block("simulink/Ports & Subsystems/In Bus Element",...
    "PortBlockCreation/InBusElement")
add_block("simulink/Ports & Subsystems/Out Bus Element",...
    "PortBlockCreation/OutBusElement")

To create a port when you add an In Bus Element or Out Bus Element block, set CreateNewPort to "on".

add_block("simulink/Ports & Subsystems/In Bus Element",...
    "PortBlockCreation/InBusElement1",CreateNewPort="on")
add_block("simulink/Ports & Subsystems/Out Bus Element",...
    "PortBlockCreation/OutBusElement1",CreateNewPort="on")

When you do not set CreateNewPort to "on", the behavior varies.

  • Suppose you have ports named InBus and OutBus. The added block corresponds with one of these ports. If you specify a port name when you add the block, the software renames the InBus or OutBus port to use the specified port name.

  • Suppose you have only custom-named ports. The added block corresponds with a new port. If you specify a port name when you add the block, the new port uses the specified port name.

Add Blocks for New Port Elements

In Bus Element and Out Bus Element blocks let you select multiple elements from an input port and connect multiple elements to an output port.

To add an element to an In Bus Element block, copy an existing block that corresponds with the port and specify a unique name for the new element.

add_block("PortBlockCreation/InBusElement",...
    "PortBlockCreation/InBusElement2",Element="signal2")

To add an element to an Out Bus Element block, copy an existing block that corresponds with the port. Optionally, specify a unique element name.

add_block("PortBlockCreation/OutBusElement",...
    "PortBlockCreation/OutBusElement2")

Duplicate Blocks for Input Ports

Multiple blocks in a block diagram can use the same input from an input port. Duplicating a block that corresponds with an input port can simplify line routing.

To duplicate an In1 block, copy the block from the block diagram and set CopyOption to "duplicate".

add_block("PortBlockCreation/In1",...
    "PortBlockCreation/In1Dup",CopyOption="duplicate")

To duplicate an In Bus Element block, copy the block.

add_block("PortBlockCreation/InBusElement",...
    "PortBlockCreation/InBusElementDup")

The duplicate blocks use the same port number as the original blocks. For name-based ports, the block selects the same element from the same port as the original block.

Input Arguments

collapse all

Path of block to add, specified as a string or character vector. The block can be a library block, or a block in a model. The block you add has the same parameter values as the source block you specify.

To get the path of a block from a built-in library, take these steps:

  1. Open the library from which you want to add the block.

    You can do so using the quick insert menu. Open the quick insert menu by double-clicking the model canvas. Then, enter the block name. Use your arrow keys to navigate to the desired search result. In the details pane on the right, click the name of the library. The library opens.

  2. Select the library block you want to add.

  3. Get the block path by entering this command in the MATLAB Command window.

    gcb

For more information about getting block paths, see Get Handles and Paths.

To create a block with a certain block type, specify the block type as 'built-in/blocktype', where blocktype is the programmatic block name. To get the value of the BlockType parameter for a block, see Common Block Properties. Blocks added using 'built-in/blocktype' can have different configurations than library blocks.

Multiple library blocks have SubSystem as the BlockType. When you specify the source block as "built-in/SubSystem", the new block represents a subsystem with one input and one output. For other subsystems and masked blocks, use the library block path instead of the BlockType value.

Example: "simulink/Math Operations/Gain"

Example: "built-in/Gain"

Example: "myModel/myOriginalBlock"

Data Types: string | character vector

Path of the new block, specified as a string.

For information about getting block paths, see Get Handles and Paths.

Example: "myModel/myNewBlock"

Data Types: string | character vector

Name-Value Arguments

collapse all

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: add_block("simulink/Math Operations/Gain","myModel/myNewBlock",Gain="5") adds a Gain block named myBlock with a gain value of 5.

For the add_block function, specify block parameter and property values as name-value arguments. For information about block parameters and properties, see Programmatically Specify Block Parameters and Properties.

Option to add a block with a unique name, specified as "off" or "on". When the option is turned on and the specified block name exists, the software appends a number to the end of the block name or increments the number at the end of the block name.

Example: add_block("simulink/Math Operations/Gain","myModel/myNewBlock",MakeNameUnique="on")

Data Types: string | character vector

Copy option, specified as "", "nolink", or "duplicate".

  • "nolink" — The new block does not link to the custom library that contains the source block.

  • "duplicate" — The new block duplicates an input port block and has the same port number as the source block. To branch a signal from an input port without creating a port or adding lines, duplicate the block that corresponds with the input port. For more information, see Create Duplicate Inport Blocks.

Example: add_block("myLibrary/CustomBlock","myModel/CustomBlock",CopyOption="nolink")

Example: add_block("myModel/mySubsystem/Inport","myModel/mySubsystem/InportDup",CopyOption="duplicate")

Data Types: string | character vector

Option to add a block for a new bus element port, specified as "off" or "on". The source block must be an In Bus Element or Out Bus Element block.

When you turn the option on, you can optionally specify the name of the new port with the PortName parameter. By default, the name of the new port is a combination of the port name from the copied block and a number. If the port name from the copied block already ends in a number, the number is incremented for uniqueness.

Setting CreateNewPort to "off" does not prevent port creation. For example, the first In Bus Element or Out Bus Element block that you add to your model creates a port. Adding a block and specifying a unique port name with PortName can also create a port.

When you set CreateNewPort to "off", you allow the added block to correspond with an existing port. For example, when you copy an In Bus Element or Out Bus Element block from your model, you can have the new block represent a new element of the port by specifying the element name with the Element parameter.

For more information on how to author bus element ports programmatically, see Programmatically Create Bus Element Ports.

Example: add_block("simulink/Ports & Subsystems/In Bus Element","myModel/InBusElement1",CreateNewPort="on")

Data Types: string | character vector

Output Arguments

collapse all

New block, returned as a handle.

Tips

To prevent overlapping blocks, specify the position of the new block with the Position block property. For more information, see Programmatically Specify Block Parameters and Properties.

Version History

Introduced before R2006a