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
  • Find a constant block and modify its value parameter with set_param
  • Get all block parameters and check your model get_param, get

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);
  • blockLocation: This is the location of the block in the Simulink library
  • blockDestnation: This is the location of the destination of your Bloc, namely your model

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:

Add a constant simulink block using the add_block MATLAB command.

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.

  • Using gcb: First let’s get the handle of the block that we’ve added to our model (mySimpleModel.slx). To do that, we’ll use the MATLAB command gcb, which will return the path of the block that we click on.
  • Using get_param: Once we have the path of the block, we can get its handle by using the get_param MATLAB command as follows:
    constantBlockHandle = get_param(gcb, 'Handle');

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);
  • Changing the value for another number: for example, let’s set the constant block to the value 3:
    set_param(constantBlockHandle, 'Value', '3');

    By executing this MATLAB command, we get:
    modify the value of a simulink constant block from MATLAB

  • Changing the value for a string: you can also use a vector instead of 3, for example [3 3 3]. However, on large models, usually the value of a Simulink block is defined as a variable. This would mean that the variable is actually a string. You can apply this command using a string, and it will work the exact same way:
    set_param(constantBlockHandle, 'Value', 'parameterValue');

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.

  • Changing the sample time of a constant block: Here is how to modify the ‘SampleTime’ parameters for a constant block:
    set_param(constantBlockHandle, 'SampleTime', '0.1');

    Which give us the following in Simulink:

    Example of using set_param on the sample time of a Simulink constant block.

  • Changing any parameter: you can also change the name of the block, output data type, the position, and so on and so forth. To do that, use the following:
    set_param(constantBlockHandle, parameter, newValue);

    The parameter argument is the parameter of your block that you can find using the method described in the previous section.

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.

  • Review your model: let’s check that the sample time definition of every block is set to ‘0.001’:
    model = 'mySimpleModel';
    % find all the paths of the blocks in your model
    blocks = find_system(model, 'LookUnderMasks', 'on', ...
        'FollowLinks', 'on', 'LoadFullyIfNeeded', 'on', 'Variants', ...
        'allVariants', 'BlockType', 'Constant');
     
    for i = 1:numel(blocks)
        constantBlockHandle = get_param(blocks{i}, 'Handle');
        block = get(constantBlockHandle);
        if ~strcmp(block.SampleTime, '0.001')
            block.SampleTime
            block.Path
        end
    end

    If you run this code you will see a list of the sample time and path of every block with a sample time different from “0.001.” Namely, the path of the constant block with the value 3 should appear since its value is “0.1”:
    get all constant with a specific sample time in Simulink

  • Modify every block programmatically: We can then use that list to define the value of the block configuration parameter to the value of the sample time that we desired:
    model = 'mySimpleModel';
    % find all the paths of the blocks in your model
    blocks = find_system(model, 'LookUnderMasks', 'on', ...
        'FollowLinks', 'on', 'LoadFullyIfNeeded', 'on', 'Variants', ...
        'allVariants', 'BlockType', 'Constant');
    for i = 1:numel(blocks)
     
        constantBlockHandle = get_param(blocks{i}, 'Handle');
        block = get(constantBlockHandle);
        if ~strcmp(block.SampleTime, '0.001')
            set_param(constantBlockHandle, 'SampleTime', '0.001')
        end
    end

    By running the code above, you should get the following in Simulink (if you double-click on your constant block):
    Modify sample time of all blocks to the desired value.

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