使用 Timer 在 GUI 中更新图表会创建一个新的图形和轴

Updating chart in a GUI with Timer creates a new figure & axes

我正在尝试制作一个程序,每隔一段时间显示一次来自经纪人的实时图表数据。该时间段可以是例如 5 秒或 15 分钟。

我做了一个GUI和一个Timer。当程序启动时,第一个绘图进入 GUI 中的 axes。但是,所有更新的图(来自计时器)都变成了一个新图形(只有一个),但没有进入GUI中的图形。

附件是一些代码:

这在 GUI .m 文件的 openingFcn

handles.timer = timer(...
'ExecutionMode', 'fixedRate', ...   % Run timer repeatedly
'Period', 5, ...                % Initial period is 5 sec.
'TimerFcn', {@updateChart,hObject}); % Specify callback
guidata(hObject,handles)
axes(handles.axes1);

candle(highBid, lowBid, closeBid, openBid);

start(handles.timer);

和函数updateChart:

function updateChart(hObject,eventdata,hfigure)
% Get new data, one candle at a time
...
% How many times the chart has already updated
handles = guidata(hfigure);
k = handles.timer.TasksExecuted;

...

% Draw (update) the chart
hold on;
axes(handles.axes1);
candle(highBid, lowBid, closeBid, openBid); % this will be plotted in a new figure !

关于如何在 GUI 上更新图表的任何建议window?

我找到了解决方法。事实上,任何一种高级绘图功能都会发生同样的事情。我不得不使用 plot 函数重现您的问题,并且行为与您描述的一样。

简答:

您必须将 figureHandleVisibility 设置为 on(而不是默认设置 callback) .如果您正在使用 GUIDE,则必须直接在 GUIDE 的图 属性 检查器中设置它(出于某些不明确的原因,如果稍后设置它不起作用在初始化代码中):

此设置将使 timer 回调具有 figure 子对象的可见性,因此 plot 命令不会决定创建一组新的 axes & figure遇到隐形人时


注一:

当使用右目标句柄指定 plot 命令时,绘图总是在右 axes 中刷新。对于支持在参数中传递目标 axes 的图形函数,语法:

% infallible syntax (when allowed)
plot( data2plot , 'Parent',targetAxesHandle)

总是比您使用的更好(设置一个 axes 活动然后在当前活动 axes 中绘制)

% this syntax may fail to plot in the right "axes" somtimes, as you observed
axes(targetAxesHandle);
plot( data2plot )

现在阅读你的特定绘图函数 candle 的文档,我没有找到你可以将 axes 句柄传递给它的线索,所以对于这种类型的函数,你必须求助于在此 post 之上给出的解决方案。 但是,如果您向工具箱的作者提供一些反馈,我强烈建议您告诉他们这个重要的缺失功能。


注二:

您不必在每个情节之前调用 hold on。如果你知道你总是"add"到剧情,你可以在初始化代码中设置一次:

set(handles.axes1,'Nextplot','add') % set "Hold on" permanently for "axes1"

如果你想解除锁定,只需设置:

set(handles.axes1,'Nextplot','replace') % set "Hold off" permanently for "axes1"