Main Content

动态封装对话框

您可以为封装模块创建对话框,这些对话框的显示会根据用户输入而发生变化。以这种方式变化的封装对话框的特性包括:

参数控制项的可见性:更改参数可能会导致另一个参数的控制项出现或消失。当某个控件显示或消失时,对话框会分别随之扩展或收缩。

参数控制项的启用状态:更改参数可能会导致另一个参数的控制项允许或禁止输入。禁用的控件将灰显以指示其显示已禁用。

参数值:更改封装对话框参数可能导致相关封装对话框参数设置为适当的值。

注意:您不能在封装回调中添加、删除或修改封装参数。

创建动态封装对话框需要结合使用封装编辑器和 set_param 命令。具体而言,您需要使用封装编辑器定义对话框的参数,包括静态和动态参数。对于每个动态参数,您需要输入一个回调函数,用于定义对话框如何响应对该参数所做的更改。该回调函数会进而使用 set_param 命令设置封装参数,这些封装参数影响对话框上其他控件的外观和设置。最后,您保存包含封装子系统的模型或库,以完成动态封装对话框的创建。

浏览模型

此示例中有两个子系统模块 Making a Parameter InvisibleDisabling a Parameter,用于更改封装参数的可见性和禁用封装参数。

open_system("slexMaskDDGExample.slx");

更改封装参数的可见性

本节引用子系统模块 Making a Parameter Invisible 进行解释。此示例说明如何根据封装参数的值来更改另一个封装参数的可见性。Enable Bias 的值通过选中或清除 Enable Bias 复选框来设置。它用于更改 Bias Value 参数的可见性。

要更改封装参数的可见性,请执行以下操作:

1.创建两个编辑参数 Gain ValueBias。创建一个复选框参数 Enable Bias

2.在代码窗格的初始化部分使用以下代码。

function initialization()
%In the Initialization command, the variable 'enableBias' corresponding to
the checkbox returns the following values:
unchecked = 0
checked = 1
% If the box is unchecked, we set the bias value to zero
if ~enableBias
  biasValue = 0;
end
% Othewise, the value from the mask parameter is used
end

3.enableBias 的参数回调部分使用以下代码。

function enableBias_callback()
% Control parameter visiblity. Get the value of the Enable Bias checkbox
% unchecked = 'off'
% checked = 'on'
enab = get_param(gcb,'enableBias');
% Set the 'MaskVisibilities' property for each parameters
if strcmp(enab,'on')
  set_param(gcb,'MaskVisibilities',{'on','on','on'});
else
  set_param(gcb,'MaskVisibilities',{'on','on','off'});
end
% Note that the value of the bias is not handled here.
% See the Initialization tab
end

4.双击子系统模块 Making a Parameter Invisible。取消选中 Enable Bias 参数,请注意 Bias 参数不可见。

禁用封装参数

此示例中引用子系统模块 Disabling a Parameter。此示例说明如何基于封装参数的值禁用另一个封装参数。复选框 Enable Bias 用于禁用 Bias Value 参数,该参数呈灰显。

要禁用封装参数,请执行以下操作:

1.创建两个编辑参数 Gain ValueBias。创建一个复选框参数 Enable Bias

2.在代码窗格的初始化部分使用以下代码。

function initialization()
% In the Initialization command, the variable 'enableBias' corresponding to
% the checkbox returns the following values:
% unchecked = 0
% checked = 1
If the box is unchecked, we set the bias value to zero
 if ~enableBias
     biasValue = 0;
end
% Othewise, the value from the mask parameter is used
 end

3.enableBias 的参数回调部分使用以下代码。

function enableBias_callback()
% Enable and disable parameters
% Get the value of the Enable Bias checkbox
% unchecked = 'off'
% checked = 'on'
 enab = get_param(gcb,'enableBias');
Set the 'MaskEnables' property for each parameters
if strcmp(enab,'on')
    set_param(gcb,'MaskEnables',{'on','on','on'});
else
    set_param(gcb,'MaskEnables',{'on','on','off'});
end
% Note that the value of the bias is not handled here.
% See the Initialization tab
end

4.双击子系统模块 Disabling a Parameter。取消选中 Enable Bias 参数,请注意 Bias 参数灰显。

另请参阅

动态封装子系统|