Matlab 匿名回调函数参数
Matlab anonymous callback function arguments
根据 Matlab 中模型视图控制器 GUI 的 this 示例,我有一个关于匿名函数回调输入参数的问题
这是一个创建 gui 句柄并将它们作为输入参数传递给 onChanged 回调函数的视图函数。
function handles = View_TimeDomain(m)
%VIEW a GUI representation of the signal model
% build the GUI
handles = initGUI();
onChangedF(handles, m); % populate with initial values
% observe on model changes and update view accordingly
% (tie listener to model object lifecycle)
addlistener(m, 'f', 'PostSet', ...
@(o,e) onChangedF(handles,e.AffectedObject));
end
我不太明白的第一件事是,根据 Matlab 文档,第一个参数必须是事件的来源,第二个参数必须是事件数据 (Matlab doc1, Matlab doc2),但是在在这种情况下是 handles
。当事件被触发时,下面的 onChangedF 函数会按预期被调用。
function onChangedF(handles, model)
% respond to model changes by updating view
if ~ishghandle(handles.fig), return, end
set(handles.line, 'XData',model.t, 'YData',model.data)
set(handles.slider, 'Value',model.f);
end
但是,在这种情况下,handles 是包含使用 initGui() 而不是事件源定义的句柄的结构。
我猜这源于匿名函数的定义:
@(o,e) onChangedF(handles, e.AffectedObject)
我是否正确,假设 o
是未在 onChangedF
函数输入中使用的来源。有人可以解释为什么这种匿名函数的语法有效吗?
我认为 o
也需要成为这个特定回调函数的参数。像这样:
@(o,e) onChangedF(o, handles, e.AffectedObject)
在函数的末尾附加了额外的参数。
然后使用 ~:
避免这个未使用的参数
function onChangedF(~, handles, model)
% respond to model changes by updating view
if ~ishghandle(handles.fig), return, end
set(handles.line, 'XData',model.t, 'YData',model.data)
set(handles.slider, 'Value',model.f);
end
Anonymous functions are a subset of function handles 允许您完全定义内联函数,而不是执行其他地方存在的函数的函数句柄。
匿名函数的语法是af = @(arglist)anonymous_function
,在功能上等同于:
function af(arglist)
anonymous_function
end
这意味着您的 PostSet
回调在功能上等同于:
function PostSet(o,e)
onChangedF(handles, e.AffectedObject)
end
满足 MATLAB 的 callback definition 要求。
因为在您创建 PostSet
匿名函数时 handles
在函数范围内,所以它在匿名函数回调的范围内也是可用的。 'Variables in the Expression' section of the anonymous function documentation. This can also be visualized using the functions
函数对此进行了解释,它提供了有关函数句柄的信息:
z = 50;
fh = @(x, y) thing(y, z);
fhinfo = functions(fh);
fhworkspace = fhinfo.workspace{1}
哪个returns:
fhworkspace =
struct with fields:
z: 50
根据 Matlab 中模型视图控制器 GUI 的 this 示例,我有一个关于匿名函数回调输入参数的问题
这是一个创建 gui 句柄并将它们作为输入参数传递给 onChanged 回调函数的视图函数。
function handles = View_TimeDomain(m)
%VIEW a GUI representation of the signal model
% build the GUI
handles = initGUI();
onChangedF(handles, m); % populate with initial values
% observe on model changes and update view accordingly
% (tie listener to model object lifecycle)
addlistener(m, 'f', 'PostSet', ...
@(o,e) onChangedF(handles,e.AffectedObject));
end
我不太明白的第一件事是,根据 Matlab 文档,第一个参数必须是事件的来源,第二个参数必须是事件数据 (Matlab doc1, Matlab doc2),但是在在这种情况下是 handles
。当事件被触发时,下面的 onChangedF 函数会按预期被调用。
function onChangedF(handles, model)
% respond to model changes by updating view
if ~ishghandle(handles.fig), return, end
set(handles.line, 'XData',model.t, 'YData',model.data)
set(handles.slider, 'Value',model.f);
end
但是,在这种情况下,handles 是包含使用 initGui() 而不是事件源定义的句柄的结构。
我猜这源于匿名函数的定义:
@(o,e) onChangedF(handles, e.AffectedObject)
我是否正确,假设 o
是未在 onChangedF
函数输入中使用的来源。有人可以解释为什么这种匿名函数的语法有效吗?
我认为 o
也需要成为这个特定回调函数的参数。像这样:
@(o,e) onChangedF(o, handles, e.AffectedObject)
在函数的末尾附加了额外的参数。 然后使用 ~:
避免这个未使用的参数function onChangedF(~, handles, model)
% respond to model changes by updating view
if ~ishghandle(handles.fig), return, end
set(handles.line, 'XData',model.t, 'YData',model.data)
set(handles.slider, 'Value',model.f);
end
Anonymous functions are a subset of function handles 允许您完全定义内联函数,而不是执行其他地方存在的函数的函数句柄。
匿名函数的语法是af = @(arglist)anonymous_function
,在功能上等同于:
function af(arglist)
anonymous_function
end
这意味着您的 PostSet
回调在功能上等同于:
function PostSet(o,e)
onChangedF(handles, e.AffectedObject)
end
满足 MATLAB 的 callback definition 要求。
因为在您创建 PostSet
匿名函数时 handles
在函数范围内,所以它在匿名函数回调的范围内也是可用的。 'Variables in the Expression' section of the anonymous function documentation. This can also be visualized using the functions
函数对此进行了解释,它提供了有关函数句柄的信息:
z = 50;
fh = @(x, y) thing(y, z);
fhinfo = functions(fh);
fhworkspace = fhinfo.workspace{1}
哪个returns:
fhworkspace =
struct with fields:
z: 50