Main Content

replace_block

Replace blocks in Simulink model

Description

replBlks = replace_block(sys,current,new) replaces the blocks current in the model sys with blocks of type new.

You can use a block from a Simulink® library or from another model as the replacement block.

The replace_block function prompts you to select the blocks you want to replace from a list of blocks that match the current argument.

Before using the replace_block function:

  • Load the model sys. For more information on how to load a model, see load_system and open_system.

  • If the library containing the new block with which you want to replace the current block is not loaded, load the library. For example, to replace the current block with a Message Triggered Subsystem block, load the Simulink library by entering this command in the MATLAB® Command Window:

    load_system('simulink.slx');

Tip

Save the model before replacing blocks.

example

replBlks = replace_block(sys,Name,Value,new) replaces the blocks that match the block parameters specified by the Name,Value pair arguments. You can also use find_system Name,Value pairs to qualify the search for blocks to replace.

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.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

With the replace_block function, you can use block parameter and value pairs. For a list of all the block parameters, see Common Block Properties and Programmatically Specify Block Parameters and Properties.

To specify additional information about the search for blocks to replace, you use find_system Name,Value pairs before the block parameters. For example, you can use 'CaseSensitive','off' to make the search for blocks case insensitive or 'FollowLinks','on' to follow links into library links. See find_system for that list of Name,Value pairs.

example

replBlks = replace_block(___,'noprompt') replaces the blocks without prompting you to select them from a dialog box.

example

Examples

collapse all

Open the example. Then, load the vdp model.

load_system("vdp");

Replace Gain blocks with Integrator blocks.

A dialog box prompts you to select the blocks you want to replace.

With vdp/Mu selected in the dialog box, click OK.

RepNames = replace_block("vdp","Gain","Integrator");

Replace Scope blocks with To Workspace blocks.

A dialog box prompts you to select the blocks you want to replace.

With vdp/Scope selected in the dialog box, click OK.

RepNames = replace_block("vdp","Scope","simulink/Sinks/To Workspace");

To replace blocks in the model with blocks from the Simulink Extras library, load the Simulink Extras library.

load_system("simulink_extras.slx");

Replace Integrator blocks with Transfer Fcn (with initial states) blocks from the Simulink Extras library.

A dialog box prompts you to select the blocks you want to replace.

With vdp/Mu, vdp/x1, and vdp/x2 selected in the dialog box, click OK.

RepNames = replace_block("vdp", "Integrator", "simulink_extras/Additional Linear/Transfer Fcn (with initial states)");

Replace blocks in the Unlocked subsystem of the sldemo_clutch model. Replace blocks whose Gain parameter is set to bv.

Load the model sldemo_clutch.

openExample('sldemo_clutch');

In the 'Unlocked' subsystem, replace blocks whose Gain value is bv with Integrator blocks.

replace_block('sldemo_clutch/Unlocked','Gain','bv','Integrator');

A dialog box prompts you to select the blocks to replace.

With sldemo_clutch/Unlocked/VehicleDamping selected in the dialog box, click OK.

Open the example. Then, load the f14 system.

load_system('f14')

Replace Gain blocks with Integrator blocks. The command returns the block paths of the Gain blocks the software finds, and then replaces them.

repl = replace_block('f14','Gain','Integrator','noprompt')
repl = 13x1 cell
    {'f14/Aircraft...'     }
    {'f14/Aircraft...'     }
    {'f14/Aircraft...'     }
    {'f14/Aircraft...'     }
    {'f14/Controller/Gain' }
    {'f14/Controller/Gain2'}
    {'f14/Controller/Gain3'}
    {'f14/Gain'            }
    {'f14/Gain1'           }
    {'f14/Gain2'           }
    {'f14/Gain5'           }
    {'f14/Nz pilot...'     }
    {'f14/Nz pilot...'     }

Select a block that is a library link. Follow the library links and replace Gain blocks with Integrator blocks within the libraries.

 replace_block(gcb, 'FollowLinks', 'on', 'BlockType', 'Gain', 'Integrator', 'noprompt')

Input Arguments

collapse all

Name of model whose blocks to replace, specified as a character vector. If you specify a model, the command replaces all blocks that match in the model. If you specify a subsystem, the command replaces blocks in that subsystem and below.

Example: 'vdp', 'sldemo_fuelsys/fuel_rate_control'

Type of block to replace, specified as a BlockType or MaskType value. To find out the block type, select the block and, at the command prompt, enter:

get_param(gcb,'BlockType')

For masked blocks, to find out the mask type, select the block and enter:

get_param(gcb,'MaskType')

Block to replace the current block, specified in one of these forms:

  • BlockType value of the replacement block. Specifying this value uses a library block as the replacement block.

  • MaskType value of the replacement block. Specifying this value uses a library block as the replacement block.

  • Library path of the replacement block, for example, 'simulink/Sinks/To Workspace'. Hover over the block in the library to see the library path.

  • Block path name of a block from a different model, for example, 'vdp/Mu'. Use this value to reuse an instance of a block from another model in your model.

Output Arguments

collapse all

Blocks returned by the current argument, returned as a cell array of character vectors. The function returns the values regardless of whether you complete the replacement.

Version History

Introduced before R2006a