| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → Simulink Design Verifier |
| Contents | Index |
| Learn more about Simulink Design Verifier |
| On this page… |
|---|
Basic Workflow for Defining Custom Block Replacements |
To replace certain blocks in your model in a way that the factory-default block replacement rules do not handle, create custom block replacement rules by completing the following tasks:
A replacement block can be one of the built-in blocks in the Simulink model library or a block in a user-created library.
In the Simulink Design Verifier software, replacement blocks have the following restrictions:
They must be built-in blocks or subsystems.
They cannot be Model blocks, nor can they include any Model blocks.
They must reside in a block library that is available on your MATLAB search path.
If the replacement block is a subsystem, any Inport and Outport blocks must have the default names (In1 and Out1).
After constructing your replacement block, write a custom block replacement rule.
In the Simulink Design Verifier software, block replacement rules have the following restrictions:
The M-file that represents a block replacement rule must include particular callbacks. The MathWorks recommends that you use the block replacement rule template as a starting point for writing a custom rule. (See Template for Block Replacement Rules.)
The M-file that represents a block replacement rule must be on the MATLAB search path.
A Multiport Switch block has one control input port and one or more data input ports; the default number of data inputs is 3.

A model may have test objectives on some blocks whose output is directly or indirectly connected to the Multiport Switch block. For example, a Saturation block may send data to the control input port. If so, the analysis may create test cases that satisfy those objectives. However, those test cases may create values that are out of range for the control input port, regardless of whether the Multiport Switch block uses zero-based indexing or one-based indexing. This causes the simulation to fail.
In this example, you create a rule to replace all Multiport Switch blocks that have two data inputs and do not use zero-based indexing. The replacement block is a subsystem that has a Test Condition block that constrains the value of the control input to 1 or 2, so that the analysis does not create out-of-range data input values. This allows the analysis to satisfy the objectives on blocks that are connected to the control input port of the Multiport Switch block.
To create a user library and specify a replacement block as a masked subsystem:
In the Simulink Library Browser, select File > New > Library.
In your library, create a subsystem named myReplacementBlock to represent your replacement block. It should look like the following graphic, with several parameters set:
In the Multiport Switch block, set the Number of inputs parameter to 2.
In the Test Condition block, set the Values parameter to {[1, 2]}.

To create a mask for your subsystem, select the subsystem, right-click, and select Mask subsystem from the context menu. For information and a tutorial for creating block masks, see Working with Block Masks in the Simulink User's Guide.
For this example, the masked subsystem includes the following specifications in its Mask Editor:
The Parameters pane defines a mask parameter named InputSameDT, which replicates the behavior of the Require all data port inputs to have the same data type parameter of the underlying Multiport Switch block.

Note When you create mask parameters that control the behavior of parameters associated with their underlying blocks, specify actual parameter names as dialog box variables in the Mask Editor. For instance, InputSameDT is the actual parameter name that controls the Require all data port inputs to have the same data type parameter of the Multiport Switch block; therefore, it specifies the name of the dialog box variable in this example. |
In the Initialization pane, in the Initialization commands field, the following commands specify that the subsystem inherit the InputSameDT parameter value of the top-level model:
maskInputSameDT = get_param(gcb,'InputSameDT');
blkName = sprintf('/Multiport\nSwitch');
targetBlock = [gcb, blkName];
set_param(targetBlock,'InputSameDT',maskInputSameDT);Save your block library as custom_rule.mdl in a folder on your MATLAB search path.
To write a rule for the replacement block:
Copy the block replacement rule template
matlabroot/toolbox/sldv/sldv/sldvblockreplacetemplate.m
Save it as custom_rule_switch.m.
Rename the function, as defined on the first line of the M-file. The function name must match its file name, without the .m extension. Optionally, you can edit the comments that follow the function declaration to create your own M-file help for this rule.
In this example, the first few lines of custom_rule_switch.m declare the function and its M-file help:
function rule = custom_rule_switch %CUSTOM_RULE_SWITCH Custom block replacement rule for %the Simulink Design Verifier software % % This block replacement rule identifies Multiport % Switch blocks whose "Number of inputs" parameter % specifies '2' and "Use zero-based indexing" parameter % specifies 'off'. It replaces such blocks with an % implementation that includes a Test Condition block % on the control input signal.
Identify the type of block that you want to replace in your model by specifying its BlockType parameter as the rule.blockType object. Consider using the get_param function to obtain the value of the BlockType parameter for the block that you want to replace. Alternatively, you can determine this value by referring to Block-Specific Parameters in the Simulink Reference.
This example replaces Multiport Switch blocks. The rule.blockType object specifies the appropriate BlockType parameter:
%% Target Block Type % rule.BlockType = 'MultiPortSwitch';
Identify the replacement block by specifying its full block path name as the rule.ReplacementPath object. Consider using the gcb function as a way to get the full block path name.
This example replaces Multiport Switch blocks with the replacement block developed in Specifying Replacement Blocks. The rule.ReplacementPath object specifies the full block path name:
%% Replacement Library
%
rule.ReplacementPath = sprintf('custom_rule/myReplacementBlock');Identify the type of subsystem that the Simulink Design Verifier software uses when replacing blocks by specifying a value for the rule.ReplacementMode object. Valid values are:
Normal — When using this rule, the software replaces blocks with a copy of the subsystem specified by the rule.ReplacementPath object.
ConfigurableSubSystem — When using this rule, the software replaces blocks with a Configurable Subsystem block. With the Configurable Subsystem block, you can choose whether it represents the subsystem specified by the rule.ReplacementPath object, or the original block before its replacement.
This example replaces Multiport Switch blocks with an ordinary Subsystem block:
%% Replacement Mode % rule.ReplacementMode = 'Normal';
Identify parameter values that the replacement blocks inherit from the blocks being replaced. You achieve inheritance by mapping the parameter names in a structure. Each field of the structure represents a parameter that the replacement block inherits. Specify the value of each field using the token $original.parameter$. parameter is the name of the parameter that belongs to the original block. You can determine block parameter names by referring to Model and Block Parameters in the Simulink Reference.
The following example defines a structure named parameter that maps the InputSameDT parameter from the original Multiport Switch blocks to their replacement blocks:
%% Parameter Handling % parameter.InputSameDT = '$original.InputSameDT$'; % Register the parameter mapping for the rule rule.ParameterMap = parameter;
To define the callback functions, keep the following lines in the file:
rule.IsReplaceableCallBack = @replacementTestFunction; . . . rule.PostReplacementCallBack = @postReplacementFunction;
Customize the replacementTestFunction subfunction by specifying conditions under which the Simulink Design Verifier software replaces blocks in your model.
The following example instructs the Simulink Design Verifier software to replace only the Multiport Switch blocks whose NumInputPorts parameter is 2 and whose zeroidx parameter is off:
function out = replacementTestFunction(blockH)
% Specify the logic that determines when the Simulink Design
% Verifier software replaces a block in your model. For example,
% restrict replacements to only the blocks whose parameters
% specify particular values.
out = false;
numInputPorts = eval(get_param(blockH,'NumInputPorts'));
zeroIdx = eval(get_param(blockH,'zeroidx'));
if numInputPorts==2 && zeroIdx=='off',
out = true;
end Optionally, you can customize the postReplacementFunction subfunction to specify the actions the software performs after a block has been replaced. For an example of a postReplacementFunction subfunction, open the following file:
matlabroot/toolbox/sldv/sldv/blkrep_rule_selectorIndexVecPort_normal.m
Save the edited M-file.
![]() | Template for Block Replacement Rules | Executing Block Replacements | ![]() |

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |