Setparam on a nested block

Hello,
I'm trying to use setparam command to modify blocks in a Simulink Real-Time model through a GUI built in App Designer. I was able to change a constant block value in the "first layer" of the Simulink model by using a command like:
setparam(tg,'Constant','Value',1)
At the same time, when trying to set similar constant blocks within subsystem blocks (so lower layers of the Simulink model), I'm getting "invalid parameter" error. The command I'm using is pretty much similar but providing the path to the Constant block. Something like this:
setparam(tg,'Layer1/Layer2/Constant','Value',1)
To be honest, I was able to change parameters at the 2nd level, but not deeper than that.
Is there some sort of limitation in using this setparam command? I couldn't find any reference online but maybe you can help here.
Like, is the problem if the layer name contains spaces (e.g. "Layer 1" instead of "Layer1")? Or maybe it's really limited to one level of nesting?
I'm on Matlab 2020a.
Thanks for any help in advance! :)

1 个评论

Ok I found the solution. The block I was trying to modify was not containing a value but a variable name and this type of constant block is considered a global parameter in Simulink Real-Time. To access a global parameter, the code is the following:
setparam(tg,'Variable Name',value)

请先登录,再进行评论。

回答(1 个)

Fangjun Jiang
Fangjun Jiang 2020-8-15

0 个投票

There is no limitation regarding how deep can you go to reference the block. The only thing that might prevent you from doing this directly is that the block is inside a library link.
But first, please verify that you've done it correctly in the basic syntax
setparam(tg,'Constant','Value',1) is no good.
It should be something like this
set_param('ModelName/Layer1/ConstantBlockName','Value','1')

3 个评论

Hi Fangjun,
Thanks for the answer. What do you mean by inside a library link? I think all the blocks I tried to access are inside subsystem I built.
For the basic syntax, I was doing the same mistake as you and I spent like 2 hours before understanding it. There are actually two very similar but very different set param commands, one for non-real-time applications (https://www.mathworks.com/help/simulink/slref/set_param.html), one for real-time applications (https://www.mathworks.com/help/xpc/api/setparam.html). The difference is the underscore sign and the number of inputs. So I need the one for real-time applications. ;)
Ok, this is for Simulink Real-Time target. I don't have such a target so I can't try.
Looking at the document, it says "Hierarchical name of the originating block" but none of the examples contains hierarchical names. I believe it is true since you said it worked for blocks in your 2nd layer subsystem. I don't think there is a limitation on the number of layers. The only thing I can think of is that the block parameter has to be specified as "tunable".
It is unlikely that every block and every parameter is accessible in Simulink Real-Time target. Some could be optimized out during code generation. Make sure the block parameters you try to change are specified as "tunable".
If you still have difficulties, contact the Mathworks Tech Support.
Cool, thanks for your hints!

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 System Configuration 的更多信息

产品

版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by