How to programmatically reference a parameter value in a masked subsystem reference?

7 views (last 30 days)
Hello,
I'm working on a large project that uses hundreds of masked subsystem references. To reduce the visual complexity of the subsystem references, I'm trying to write a script that programmatically finds all constant blocks in a model and if any constant block connects to two or more destinations, delete the extra lines and add copies of the constant block next to the destination blocks.
The problem I'm encountering arises when I try to copy a constant block who's value is the name of a parameter in the subsystem mask. The relevant bit of code is:
name = get_param(constantBlock, 'Name');
value = get_param(constantBlock, 'Value');
connectedPorts = get_param(constantBlock, 'PortConnectivity');
% If there are more than 1 destination block, copy the constant block
for i = 2:length(connectedPorts.DstBlock)
newBlock = add_block('simulink/Sources/Constant', [modelName, '/' name], 'MakeNameUnique', 'on');
set_param(newBlock, 'Value', value);
% <additional code for calculating and setting the position of the new block and drawing new lines>
% ...
end
This code works nominally, but when it is passed a constant block that takes a mask parameter as value, the following error is produced:
Invalid setting in 'My_Model/MyConstant' for parameter 'Value'.
Suppose that the parameter in question is named 'myParam'. When dropping a breakpoint at the line producing the error, I noticed that I can manually assign the value of the constant to 'myParam1', 'myParam2', etc. without issue. The problem only arises when I try to assign the value to 'myParam', which is a parameter in the subsystem's mask.
So far, I have tried to assign the value of myParam programmatically, i.e.
assignin('base', 'MyParam', 42);
assignin('caller', 'MyParam', 42);
and to wrap the parameter name in extra quotes
set_param(newBlock, 'Value', ['''' value '''']);
But neither method helped.
I understand that I can programmatically remove the parameter from the mask, set the value of the constant block and then add the parameter back into the mask, but I wonder if there is no better way of solving the issue?
Thankful for any assistance.
// Viktor
  3 Comments
Shivangi
Shivangi on 8 Sep 2025
Hi Viktor,
I understand that the issue is that even after removing a mask parameter, Simulink still raises an Invalid setting for parameter 'Value' error because the mask workspace is only refreshed when the model is reloaded or updated. This explains why closing and reopening the model temporarily solves the problem.
While closing and reopening the model forces a refresh, a lighter workaround is to trigger re-evaluation programmatically using:
maskObj = Simulink.Mask.get(modelName);
maskObj.refresh;
For broader compatibility across versions:
set_param(modelName, 'SimulationCommand', 'update');
These approaches prompt Simulink to reprocess the mask workspace without requiring a full reload.
Alternatively, cloning the original Constant block using add_block with the source block path preserves the reference to the mask parameter, avoiding the need for re-evaluation altogether.
Hope this solves your query!
Some useful documentation:
Viktor
Viktor on 11 Sep 2025
Directly copying the old block works! Thank you!
For future readers finding this thread, I used:
newBlock = add_block(oldBlock, [modelName, '/' name], 'MakeNameUnique', 'on');

Sign in to comment.

Answers (0)

Categories

Find more on Programmatic Model Editing in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!