Add, Copy, Replace, and Delete Blocks Programmatically - MATLAB & Simulink (original) (raw)

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 namedmyBlock 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, theadd_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 theStep 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:

  1. Open the quick insert menu by double-clicking the model canvas.
  2. Enter the block name.
  3. Use your arrow keys to select the block in the quick insert menu search results.
  4. On the details pane, click the name of the library that contains the block. The library opens. The block is highlighted in yellow.
  5. Select the block.
  6. Get the path of the current block using this command.

Quick insert menu with the word "derivative" in the search box, the Derivative block selected in the search results, and the pointer paused on the name of the Simulink library on the details pane

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, andh is the handle of the new block.

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 namedmyModel. Name the blockmyBlock.For information about how to get the path of a library block, see the previous section. The path of the Ramp library block issimulink/Sources/Ramp.h = add_block("simulink/Sources/Ramp",... "myModel/myBlock",MakeNameUnique="on");
Duplicate a block. Duplicate a block named myBlock in a model namedmyModel. Name the duplicatemyBlock2.h = add_block("myModel/myBlock","myModel/myBlock2",MakeNameUnique="on");
Copy a block into another model, library, or subsystem. Copy a block named myBlock from a model namedmyModel into a model named myModel2. Name the copymyBlock2.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 myBlock to a model named myModel. Set the slope of the ramp to2 and the start time to 5. Specify the parameter names and values as name-value arguments at the end of the list of input arguments.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, wherecurrent 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 SubSystem. To specify the replacement as a block type, the block type must be unique to one block. Replace all Constant blocks in a model namedmyModel with Sine Wave blocks.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 namedmyModel with Ramp blocks.The block type of the Ramp block is SubSystem. There are multiple blocks with the block type SubSystem. Specify the replacement block using the library block path,simulink/Sources/Ramp, instead of the block type.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 Constant blocks in a model namedmyModel with copies of a block named myBlock from a model namedmyModel2.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 myModel that have an amplitude of 2 and a phase of 0.5 withSine Wave blocks.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 myModel whose name contains the word filter with Sine Wave blocks.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".*filter.*" to ".*(?i)filter.*". For more information about regular expressions, see Regular Expressions.
Replace a block using its handle or path. Replace a block named myBlock in a model namedmyModel with a Sine Wave block.Use the block path of myBlock to get the block handle.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 myModel, replace the block namedmyBlock1, the block named myBlock2, and the block named myBlock3 with Sine Wave blocks.Use the block paths of myBlock1,myBlock2, and myBlock3 to get the block handles. Store the handles in a vector.paths = "myModel/myBlock"+(1:3)'; hb = getSimulinkBlockHandle(cellstr(paths));Write a function that replaces a block with the handle h with aSine Wave block.myFunc = @(h)replace_block("myModel","Handle",h,"Sin","noprompt");Use the arrayfun function to run themyFunc function on each element in the vector of handles.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 handleh.delete_block(h)
Delete multiple blocks. Delete the two blocks with the handles h1 andh2.delete_block([h1;h2])
Delete model contents. Delete the contents of a model with the handleh.Simulink.BlockDiagram.deleteContents(h)
Delete subsystem contents. Delete the contents of a subsystem with the handleh.Simulink.SubSystem.deleteContents(h)

See Also

add_block | replace_block | delete_block | Simulink.BlockDiagram.deleteContents | Simulink.SubSystem.deleteContents

Topics