In Simulink, you can retrieve block parameters programmatically using the get_param function in the MATLAB command window or in a script. For visual inspection and editing, you can use the Block Parameters dialog box, the Property Inspector, or the Model Data Editor. Understanding the distinction between programmatic and dialog parameter names is key to accessing block-specific and common properties accurately.
Programmatic access using get_param
The most versatile method for accessing block parameters is the get_param function. It allows you to query a block's properties, including those not visible in the standard dialog boxes, from the MATLAB command prompt or within a script.
The basic syntax is get_param('BlockPath', 'ParameterName').
Step 1: Identify the block path
The block path is the hierarchical location of the block, starting from the model name.
- For a top-level block, the path is
modelName/blockName. - For a block inside a subsystem, the path is
modelName/SubsystemName/blockName.
You can also use the gcb (get current block) command to get the path of the currently selected block.
**Example:**Suppose you have a model named myModel with a Gain block named My Gain inside a subsystem named Controller. The full path would be myModel/Controller/My Gain.
Step 2: Find the programmatic parameter name
The name displayed in the Block Parameters dialog box is often different from the programmatic name used by get_param.To find a block's programmatic parameter names, use get_param with the following properties:
DialogParameters: Returns a structure of the parameters shown in the block's dialog box.ObjectParameters: Returns a structure containing a more extensive list of block-specific and common properties.
**Example:**To find the programmatic names for the myModel/Controller/My Gain block:
params = get_param('myModel/Controller/My Gain', 'DialogParameters');
fieldnames(params)
Use code with caution.
The result will show the names of the parameters, such as 'Gain'.
Step 3: Get the parameter value
Once you have the block path and the programmatic parameter name, you can retrieve its value.
**Example:**To get the value of the Gain parameter from the myModel/Controller/My Gain block:
gain_value = get_param('myModel/Controller/My Gain', 'Gain');
disp(['The Gain value is: ', gain_value]);
Use code with caution.
Visual inspection and editing in the Simulink Editor
For interactive use, Simulink offers several graphical interfaces to view and modify block parameters.
1. Block Parameters dialog box
The most common method is to double-click a block to open its dialog box. Here, you can see and edit parameters directly. For complex or masked subsystems, this dialog box may present a user interface designed by the block creator.
2. Property Inspector
The Property Inspector provides a single, dockable pane for viewing and editing parameters and properties of any selected model element.
- To open it: Navigate to the Modeling tab and select Property Inspector in the Design section.
- Usage: Select any block, and the Property Inspector will update to show its parameters and properties in the Parameters tab.
3. Model Data Editor
The Model Data Editor allows for batch editing of block parameters across a model.
- To open it: Navigate to the Modeling tab and select Model Data Editor.
- Usage: Go to the Parameters tab. The editor lists many block parameters in a table, allowing you to sort, filter, and change values for multiple blocks at once.
Advanced considerations
Masked blocks
For masked blocks (which appear as subsystems but have custom dialogs), the DialogParameters property will show the mask parameters rather than the underlying block parameters. To get the parameters for the inner blocks, you must use their full path.
To access a masked parameter's value, use the 'value@parameterName' syntax in get_param.**Example:**If a masked subsystem vdp/Mu has a mask parameter named gain, you can get its value with:
mu_gain = get_param('vdp/Mu', 'value@gain');
Use code with caution.
Tunable parameters and workspaces
When setting block parameters, you can use variables from different workspaces, such as the MATLAB base workspace or the model workspace. This allows you to tune parameters programmatically during a simulation run or experiment with different values.
- Use
get_paramandset_paramto manage these variables. - For advanced parameter sweeping, consider using
Simulink.SimulationInputobjects.
Simulink.findBlocksOfType
When you need to find all blocks of a specific type to get their parameters, the Simulink.findBlocksOfType function is useful. You can use it to find blocks and then iterate through their handles to get parameter values.
**Example:**To find and display the Gain value for all Gain blocks in a model named myModel:
gain_blocks = Simulink.findBlocksOfType('myModel', 'Gain');
for i = 1:numel(gain_blocks)
block_path = get_param(gain_blocks(i), 'Name');
gain_value = get_param(gain_blocks(i), 'Gain');
disp(['Block: ', block_path, ', Gain: ', gain_value]);
```end
Use code with caution.