编写封装回调
函数 customsat_callback.m 包含自定义饱和模块封装参数对话框的封装回调代码。此函数通过调用 feval 来调用与每个封装参数对应的局部函数。
下面的局部函数基于选择的饱和上界模式来控制饱和上界字段的可见性。回调首先通过使用属性名称 MaskValues 调用 get_param 来获取所有封装参数的值。如果回调只需要一个封装参数的值,它可以使用具体的封装参数名称(例如 get_param(block,'upMode'))来调用 get_param。因为此示例需要两个封装参数值,所以它使用 MaskValues 属性以减少对 get_param 的调用。
然后,回调通过使用属性名称 MaskVisbilities 调用 get_param 来获取封装参数的可见性。此调用返回一个字符向量元胞数组,指示每个封装参数的可见性。回调基于选择的饱和上界模式更改封装可见性的值,然后更新端口标签文本。
最后,回调使用 set_param 命令更新模块的 MaskDisplay 属性,以标记模块的输入端口。
function customsat_callback(action,block)
% CUSTOMSAT_CALLBACK contains callbacks for custom saturation block
% Copyright 2003-2007 The MathWorks, Inc.
%% Use function handle to call appropriate callback
feval(action,block)
%% Upper bound callback
function upperbound_callback(block)
vals = get_param(block,'MaskValues');
vis = get_param(block,'MaskVisibilities');
portStr = {'port_label(''input'',1,''uSig'')'};
switch vals{1}
case 'No limit'
set_param(block,'MaskVisibilities',[vis(1);{'off'};vis(3:4)]);
case 'Enter limit as parameter'
set_param(block,'MaskVisibilities',[vis(1);{'on'};vis(3:4)]);
case 'Limit using input signal'
set_param(block,'MaskVisibilities',[vis(1);{'off'};vis(3:4)]);
portStr = [portStr;{'port_label(''input'',2,''up'')'}];
end
if strcmp(vals{3},'Limit using input signal'),
portStr = [portStr;{['port_label(''input'',',num2str(length(portStr)+1), ...
',''low'')']}];
end
set_param(block,'MaskDisplay',char(portStr));
最后一次调用 set_param 会在 MATLAB S-Function custom_sat.m 中调用 setup 函数。因此,可以修改 setup 函数,以便基于封装参数值而不是基于 S-Function 参数值来设置输入端口数。对 setup 函数的此类更改可使 2 级 MATLAB S-Function 模块上的端口数与封装参数对话框中显示的值保持一致。
修改后的 MATLAB S-Function custom_sat_final.m 包含下面的新 setup 函数。如果您是按照本教程中的步骤进行操作的,请打开该文件并将其保存到您的工作文件夹中。
%% Function: setup ===================================================
function setup(block)
% Register original number of ports based on settings in Mask Dialog
ud = getPortVisibility(block);
numInPorts = 1 + isequal(ud(1),3) + isequal(ud(2),3);
block.NumInputPorts = numInPorts;
block.NumOutputPorts = 1;
% Setup port properties to be inherited or dynamic
block.SetPreCompInpPortInfoToDynamic;
block.SetPreCompOutPortInfoToDynamic;
% Override input port properties
block.InputPort(1).DatatypeID = 0; % double
block.InputPort(1).Complexity = 'Real';
% Override output port properties
block.OutputPort(1).DatatypeID = 0; % double
block.OutputPort(1).Complexity = 'Real';
% Register parameters. In order:
% -- If the upper bound is off (1) or on and set via a block parameter (2)
% or input signal (3)
% -- The upper limit value. Should be empty if the upper limit is off or
% set via an input signal
% -- If the lower bound is off (1) or on and set via a block parameter (2)
% or input signal (3)
% -- The lower limit value. Should be empty if the lower limit is off or
% set via an input signal
block.NumDialogPrms = 4;
block.DialogPrmsTunable = {'Nontunable','Tunable','Nontunable','Tunable'};
% Register continuous sample times [0 offset]
block.SampleTimes = [0 0];
%% -----------------------------------------------------------------
%% Options
%% -----------------------------------------------------------------
% Specify if Accelerator should use TLC or call back into
% MATLAB script
block.SetAccelRunOnTLC(false);
%% -----------------------------------------------------------------
%% Register methods called during update diagram/compilation
%% -----------------------------------------------------------------
block.RegBlockMethod('CheckParameters', @CheckPrms);
block.RegBlockMethod('ProcessParameters', @ProcessPrms);
block.RegBlockMethod('PostPropagationSetup', @DoPostPropSetup);
block.RegBlockMethod('Outputs', @Outputs);
block.RegBlockMethod('Terminate', @Terminate);
%endfunction
custom_sat_final.m 中的局部函数 getPortVisibility 使用饱和界限模式构造一个标志,该标志被传递回 setup 函数。setup 函数使用此标志确定必需的输入端口数。
%% Function: Get Port Visibilities =======================================
function ud = getPortVisibility(block)
ud = [0 0];
vals = get_param(block.BlockHandle,'MaskValues');
switch vals{1}
case 'No limit'
ud(2) = 1;
case 'Enter limit as parameter'
ud(2) = 2;
case 'Limit using input signal'
ud(2) = 3;
end
switch vals{3}
case 'No limit'
ud(1) = 1;
case 'Enter limit as parameter'
ud(1) = 2;
case 'Limit using input signal'
ud(1) = 3;
end