Add, Copy, Replace, and Delete Blocks Programmatically
You can programmatically populate a model, library, or subsystem by adding, copying, replacing, and deleting blocks using the functions listed on this page. The model, library, or subsystem must be loaded. For information about how to load models, libraries, and subsystems, see Create, Load, Open, Save, and Close Models Programmatically.
For example, this command adds a Step block named
myBlock
to a model named myModel
.
add_block("simulink/Sources/Step","myModel/myBlock");
You can specify which block you want a function to add, copy, replace, or delete as a
function input argument, typically the first input argument. In this example, the
add_block
function has two input arguments, both specified as block
paths. The first argument specifies which block to add, and the second specifies the location
and name of the new block. To get the path of a Library Browser block such as the
Step block in this example, see the next section. For more information, see
the function documentation and Get Handles and Paths.
To add, copy, replace, and delete blocks, the model, library, or subsystem, the model, library, or subsystem must be loaded. For information about how to load models, libraries, and subsystems, see Create, Load, Open, Save, and Close Models Programmatically.
For information about how to position blocks or auto-arrange your model diagram, see Configure Model Layout Programmatically.
Get Path of Library Block
To get the path of a Library Browser block:
Open the quick insert menu by double-clicking the model canvas.
Enter the block name.
Use your arrow keys to select the block in the quick insert menu search results.
On the details pane, click the name of the library that contains the block. The library opens. The block is highlighted in yellow.
Select the block.
Get the path of the current block using this command.
gcb
Add and Copy Blocks
To add a block, duplicate a block, or copy a block to a different model, library, or
subsystem, use the add_block
function. The function syntax
takes this basic form, where src
specifies the block you want to add or
copy, dst
specifies the name and location of the new block, and
h
is the handle of the new block.
h = add_block(src,dst)
If the destination of the new block already contains a block with the name you specify for the new block, you get an error. To give the block that you add a unique name, use this syntax.
h = add_block(src,dst,MakeNameUnique="on")
If the name is not unique, the software adds a number to the name. If the name already ends in a number, the software changes the number. Since the name of the new block might not match the name you specify, do not use the name you specified in subsequent code. Use the block handle instead.
The table shows examples of adding and copying blocks. For more syntaxes and examples, see the function documentation.
Action | Example |
---|---|
Add a block from the library. | Add a Ramp block from the library to a model named
For information about how to get the path
of a library block, see the previous section. The path of the Ramp
library block is
h = add_block("simulink/Sources/Ramp",... "myModel/myBlock",MakeNameUnique="on"); |
Duplicate a block. | Duplicate a block named h = add_block("myModel/myBlock","myModel/myBlock2",MakeNameUnique="on"); |
Copy a block into another model, library, or subsystem. | Copy a block named h = add_block("myModel/myBlock",... "myModel2/myBlock2",MakeNameUnique="on"); |
Take any of the other actions and set parameter values of the new block in a single command. | Add a Ramp block named h = add_block("simulink/Sources/Ramp","myModel/myBlock",... slope="2",start="5",MakeNameUnique="on") |
Replace Blocks
To replace a block, use the replace_block
function. The function syntax
takes this basic form, where sys
specifies the model or subsystem in
which you want to make the replacement, the Name,Value
arguments specify
the parameter values that uniquely identify the blocks to replace, new
specifies the replacement block or block type, and h
is the handle of the
new block. The replacement block can be a library block or a block from another model or
subsystem.
h = replace_block(sys,Name1,Value1,Name2,Value2,...
NameN,ValueN,new)
To replace all blocks of a specific block type, you can use this shorter syntax, where
current
is the block type you want to replace.
h = replace_block(sys,current,new)
Specify block types using their programmatic names. For example, specify Sine
Wave blocks as "Sin"
. To get the programmatic name of a block
type, select the block of that type in a model, and then enter this command in the
MATLAB® Command Window.
get_param(gcb,"BlockType")
By default, when you run the replace_block
function, a dialog box
opens. In the dialog box, you can opt to replace all the blocks you specified in the command
or to manually select a subset of those blocks to replace. To suppress the dialog box and
replace all blocks you specified in the command, use the noprompt
option.
h = replace_block(sys,current,new,"noprompt")
The table shows examples of how to programmatically replace blocks. The blocks being replaced are the same for all three examples, but the replacement blocks are different for each example. For more syntaxes and examples, see the function documentation.
Examples of How to Specify Replacement Blocks
Action | Example |
---|---|
Replace all blocks of one type with blocks of another type. Multiple blocks can have the same type, for example, the block
type | Replace all Constant blocks in a model named
h = replace_block("myModel","Constant","Sin","noprompt") |
Replace all blocks of one type with instances of a library block. | Replace all Constant blocks in a model named
The
block type of the Ramp block is h = replace_block("myModel","Constant",... "simulink/Sources/Ramp","noprompt") |
Replace all blocks of one type with copies of a block from a model or subsystem. | Replace all h = replace_block("myModel","Constant",... "myModel2/myBlock","noprompt") |
In this table, the replacement blocks are the same for all four examples, but the blocks being replaced are different for each example. You can combine the syntaxes for the blocks being replaced in this table with any of the syntaxes for the replacement blocks from the previous table.
Examples of How to Specify Which Blocks To Replace
Action | Example |
---|---|
Replace all blocks that have specific parameter values. | Replace all blocks in a model named h = replace_block("myModel","Amplitude","2","Phase","5"... "Sin","noprompt") |
Replace all blocks whose name contains a keyword. | Replace all blocks in a model named h = replace_block("myModel","RegExp","on","Name",".*filter.*",... "Sin","noprompt") In this syntax, the keyword is
case-sensitive. To make the keyword case-insensitive, change
|
Replace a block using its handle or path. | Replace a block named Use the block path of path = 'myModel/myBlock';
hb = getSimulinkBlockHandle(path) Replace the block. h = replace_block("myModel","Handle",hb,"Sin","noprompt") |
Replace multiple blocks using their handles or paths. | In a model named Use the block paths of paths = "myModel/myBlock"+(1:3)';
hb = getSimulinkBlockHandle(cellstr(paths)); Write a
function that replaces a block with the handle myFunc = @(h)replace_block("myModel","Handle",h,"Sin","noprompt"); Use
the arrayfun(myFunc,hb) |
Delete Blocks
To delete blocks, use the delete_block
function. To delete the
contents of models, including blocks, signal lines, and annotations, use the Simulink.BlockDiagram.deleteContents
function. To delete the contents of a
subsystem, use the Simulink.SubSystem.deleteContents
function.
The table shows examples of these actions. For more syntaxes and examples, see the function
documentation. For information about how to get block handles, see Get Handles and Paths.
Action | Example |
---|---|
Delete a block. | Delete the block with the handle
delete_block(h) |
Delete multiple blocks. | Delete the two blocks with the handles delete_block([h1;h2]) |
Delete model contents. | Delete the contents of a model with the handle
Simulink.BlockDiagram.deleteContents(h) |
Delete subsystem contents. | Delete the contents of a subsystem with the handle
Simulink.SubSystem.deleteContents(h) |
See Also
add_block
| replace_block
| delete_block
| Simulink.BlockDiagram.deleteContents
| Simulink.SubSystem.deleteContents