replace_block - Replace blocks in Simulink model - MATLAB (original) (raw)

Replace blocks in Simulink model

Syntax

Description

[replBlks](#f21-64352-replBlks) = replace_block([sys](#f21-64352-sys),[current](#f21-64352-current),[new](#f21-64352-new)) replaces the blocks current in the modelsys 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 thecurrent argument.

Before using the replace_block function:

Tip

Save the model before replacing blocks.

example

[replBlks](#f21-64352-replBlks) = replace_block([sys](#f21-64352-sys),`Name,Value`,[new](#f21-64352-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 asName1=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](#f21-64352-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.

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.

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 = 13×1 cell {'f14/Aircraft↵Dynamics↵Model/Gain3'} {'f14/Aircraft↵Dynamics↵Model/Gain4'} {'f14/Aircraft↵Dynamics↵Model/Gain5'} {'f14/Aircraft↵Dynamics↵Model/Gain6'} {'f14/Controller/Gain' } {'f14/Controller/Gain2' } {'f14/Controller/Gain3' } {'f14/Gain' } {'f14/Gain1' } {'f14/Gain2' } {'f14/Gain5' } {'f14/Nz pilot↵calculation/Gain1' } {'f14/Nz pilot↵calculation/Gain2' }

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:

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