Real Technology Tools

Create a Simulink Constant Block from a Script

Sometimes in Simulink, you’ll have to write code that adds, reviews, and modifies constant blocks since it’s way too long to apply modifications manually on a very large model. In this article, you’ll learn to:

Add a Constant Block from MATLAB With add_block

You can use the MATLAB command add_block to add any block you want to your model:

add_block(blockLocation, blockDestination);

For example, if you want to add a constant block to a model named mySimpleModel.slx from MATLAB this is how to do that:

add_block('simulink/Sources/Constant', 'mySimpleModel/Constant');

You can also specify some parameters to the block you are adding. For example, you can set the value of a constant block by using the additional arguments ‘Value’ followed by the value that you want your block to have. Here’s is an example of adding a block with the value 5:

add_block('simulink/Sources/Constant', 'mySimpleModel/Constant', 'Value', '5');

Your model would then look like:

Find a Constant Block and Modify its Value Parameter

Constant Simulink: Get the Block Handle

A block handle is a unique number associated with every Simulink block in your model. Because it is unique, you know that you have the right block when you want to modify one of its parameters.

Using the handle of the block, you can modify the parameter of the block that you want with the MATLAB command set_param.

Note that in order for this to work, you need to have clicked on the constant block in your model. Otherwise, the gcb MATLAB command won’t return the path of the constant block, and the get_param command will not be able to output the handle of this block.

Keep in mind that your handle for this block will be different from the one I get, so there is no point in me showing you the output of this command.

Change the Value of a Constant Block

As stated before, at times you’ll have to change the value of a constant block in Simulink. However, when you have a large model and you need to apply a modification systematically on every constant block value, you’ll have to automate that process by using a MATLAB script to apply the modifications.

Now that you have the handle of the constant Simulink block that you’re interested in modifying, you can modify its parameters.

To modify the value of that block, use the set_param MATLAB command as the following:

set_param(constantBlockHandle, 'Value', newValue);

Constant Simulink: Other Parameters and Model Check

Get All Block Parameters

You can get all block parameters by using the MATLAB command get on the handle of the desired block.

For example, if you click on a constant block, you can use the command:

block = get(get_param(gcb, 'Handle'));

If you look at the “block” variable in your workspace, you’ll get the list of all the parameters of this block:

Example of using get to get all block parameters

Change Other Parameters of the Block

Using the same MATLAB command (set_param), you can change all of the parameters of a constant Simulink block.

For example, you might want to change the sample time of a constant block since, by default, it is set to ‘inf’. You might want to change it to other values such as the actual sample time value that you want or a parameter that you’ve defined, among others.

Check that All Constant Sample Times Are the Same

At some point, you might want to check different parameters of every single block in your model from a script (especially if it is a very large model). For example, we usually want the sample time of every constant block to be identical.

Key takeaways:

  1. Add a block from a MATLAB script using the MATLAB command add_block:
    add_block(blockLocation, blockDestination);
  2. To get the handle of a Simulink block:
    1. Click on it.
    2. Use the MATLAB command get_param:
      constantBlockHandle = get_param(gcb, 'Handle');
  3. You can get all block parameters by:
    1. Clicking on the desired block.
    2. Using the following MATLAB command:
      block = get(get_param(gcb, 'Handle'))
  4. You can change the value of a constant block from MATLAB using its handle with the set_param MATLAB command:
    set_param(constantBlockHandle, 'Value', newValue);
  5. More generically, you can change every parameter value of a Simulink block with set_param:
    set_param(constantBlockHandle, parameter, newValue);

For more information, you can refer to the following links:

🌱 If you want to learn more about the tools that helped me stop wasting time doing mindless work (such as generating Excel reports, Word documents, or creating clean and simple user interfaces) I wrote a small reference book about it:

👉 www.amazon.com/dp/B08L7FM1BB

Exit mobile version