我正在使用GUIDE在Matlab中制作一个GUI应用程序。我有一些轴,我正在上面绘制一些点的按钮点击。现在我想使用动画线在相同的轴上绘制线。要添加到动画行的数据点来自一个事件。因此,我需要在事件侦听器中添加数据点。我想知道如何做到这一点,因为事件侦听器无法访问动画行。以下是迄今为止的代码。
点击按钮时调用此功能-
function startButton_Callback(~, ~, handles)
x = randi(100, 20);
y = randi(100, 20);
plot(x, y, 'o');
la = newClass;
addlistener(la,'statusAnnouncement',@StatusListener);
这是在事件上调用的函数
function StatusListener(obj, eventData)
h = animatedline;
addpoints(h,eventData.coordinate(1),eventData.coordinate(2));
drawnow
仅显示使用绘图绘制的点。如何显示动画线条?此外,我在命令窗口中没有得到任何错误。
有几种方法可以让侦听器访问animtedline
对象。
-
您可以将
StatusListener
定义为startButton_Callback
的子函数function startButton_Callback(~, ~, handles) h = animatedline; la = newClass; addlistener(la,'statusAnnouncement',@StatusListener); %// This as a subfunction so it can "see" h function StatusListener(src, evnt) h.addpoints(evnt.coordinate(1), evnt.coordinate(2)); end end
-
通过匿名函数将
animtedline
对象传递给回调函数function startButton_Callback(~, ~, handles) h = animatedline; la = newClass; %// Use the callback but add h as an additional input argument addlistener(la, 'statusAnnouncement', @(s,e)StatusListener(s,e,h)) end %// Note the additional input argument here function StatusListener(obj, evnt, h) h.addpoints(evnt.coordinate(1), evnt.coordinate(2)) end
-
更新匿名函数的内部的
animatedline
function startButton_Callback(~, ~, handles) h = animatedline; la = newClass; %// Don't define a separate function and just do the update here addlistener(la, 'statusAnnouncement', @(s,e)h.addpoints(e.coordinate(1), e.coordinate(2))) end
-
将
animatedline
对象存储在图的appdata
的guidata
中。function startButton_Callback(~, ~, handles) h = animatedline; %// Store the handle within the appdata of the figure setappdata(gcbf, 'MyAnimatedLine', h) la = newClass; addlistener(la,'statusAnnouncement',@StatusListener); end function StatusListener(obj, evnt) %// Get the handle from the appdata of the figure h = getappdata(gcbf, 'MyAnimatedLine'); h.addpoints(evnt.coordinate(1), evnt.coordinate(2)) end