以编程方式创建掩码
我不确定这是否正是您要搜索的内容,但我举了一个示例,说明如何通过 MATLAB/Simulink 中的脚本以编程方式创建掩码。我不会使用get_param/set_param,即使使用这些命令可以获得相同的结果。我们将使用更简单、更清晰的Simulink 对象(至少恕我直言)。
对于我们的 Playground,让我们创建这个简单的子系统 (block),其中包含一个简单的常量,该常量在输出中提供一个名为 a 的变量的名称,我们想从掩码中获取该变量的名称:
查看这个块的地址。我的 simulink 模型是 mask.slx,因此我可以使用地址 mask/block(视口的左上角)来寻址这个子组,如您在此处看到的:
此时我们可以使用下面的代码为子组添加一个编辑参数框,固定a的值:
clc
clear all
% The subgroup for which we want to programmatically create a mask
block_name = 'mask/block';
% Now we can create the mask parameter programmatically as you requested
% There are two way: the old one using get_param and set_param and a more
% clear one using the Simulink interface.
% I will go with thw second one, since it is really more straightforward
% with respect to the first one.
% The first think to do is to create the mask itself
% If the mask already exist, we would get an error, thus we can avoid it by
% checking if it already exist. This is something that you should check out.
mask_hdl = Simulink.Mask.create(block_name);
% mask_hdl = Simulink.Mask.get(block_name); % For use an existing mask
% Now we are ready to create the mask parameters:
edit_a_hdl = mask_hdl.addParameter( ...
'Type', 'edit', ...
'Prompt', 'Sets constant variable', ...
'Name', 'a');
edit_a_hdl.Value = '10';
运行脚本,代码将被屏蔽并设置变量,如您在此处看到的:
还有更多信息on this topic here。
以编程方式为屏蔽块设置参数
现在假设您像以前一样完成了操场,并且像上一张图像一样遮盖了子组。您可以通过get_param 和set_param 以编程方式(或获取)在掩码中设置其值,如下所示:
value = get_param(block_name, 'a');
value = str2double(value); % Values should always be string!
% Thus we must convert it
set_param(block_name, 'a', sprintf('%d', value * 100));
如您所见,该值现已更新:
同样,您可以使用Simulink 对象获得相同的结果。
mask_hdl = Simulink.Mask.get(block_name);
edit_a_hdl = mask_hdl.Parameters(1); % We know its position in the struct array
value = str2double(edit_a_hdl.Value);
value = value * pi;
edit_a_hdl.Value = sprintf('%f', value);
如您所见,我们有了新的价值: