Matlab更新具有多个数据线/曲线的图 [英] Matlab update plot with multiple data lines/curves
问题描述
我想尽快更新具有多个数据线/曲线的图.我已经看到了一些使用以下方法来更新情节的方法:
I want to update a plot with multiple data lines/curves as fast as possible. I have seen some method for updating the plot like using:
h = plot(x,y);
set(h,'YDataSource','y')
set(h,'XDataSource','x')
refreshdata(h,'caller');
或
set(h,'XData',x,'YData',y);
对于一条曲线来说,效果很好,但是我不仅要更新一条曲线,还要更新多条数据曲线.我该怎么办?
For a single curve it works great, however I want to update not only one but multiple data curves. How can I do this?
推荐答案
如果使用单个plot
命令创建多个绘图对象,则plot
返回的句柄实际上是
If you create multiple plot objects with a single plot
command, the handle returned by plot
is actually an array of plot objects (one for each plot).
plots = plot(rand(2));
size(plots)
1 2
因此,您不能只是将另一个[2x2]矩阵分配给XData
.
Because of this, you cannot simply assign another [2x2] matrix to the XData
.
set(plots, 'XData', rand(2))
您可以通过以下语法将新XData的单元格数组传递给绘图.只有在单元格数组中已经有新值的情况下,这才真正方便.
You could pass a cell array of new XData to the plots via the following syntax. This is only really convenient if you already have your new values in a cell array.
set(plots, {'XData'}, {rand(1,2); rand(1,2)})
其他选项是使用新值分别更新每个图对象.就快速执行而言,立即设置所有设置并不会对性能造成太大影响,因为直到MATLAB空闲或您显式调用drawnow
时,它们才会真正呈现出来.
The other options is to update each plot object individually with the new values. As far as doing this quickly, there really isn't much of a performance hit by not setting them all at once, because they will not actually be rendered until MATLAB is idle or you explicitly call drawnow
.
X = rand(2);
Y = rand(2);
for k = 1:numel(plots)
set(plots(k), 'XData', X(k,:), 'YData', Y(k,:))
end
% Force the rendering *after* you update all data
drawnow
如果您确实要使用已显示的XDataSource
和YDataSource
方法,则可以执行此操作,但是您需要为每个绘图对象指定唯一的数据源.
If you really want to use the XDataSource
and YDataSource
method that you have shown, you can actually do this, but you would need to specify a unique data source for each plot object.
% Do this when you create the plots
for k = 1:numel(plots)
set(plots(k), 'XDataSource', sprintf('X(%d,:)', k), ...
'YDataSource', sprintf('Y(%d,:)', k))
end
% Now update the plot data
X = rand(2);
Y = rand(2);
refreshdata(plots)
这篇关于Matlab更新具有多个数据线/曲线的图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!