在Matlab axis中,如何在保持所有轴属性的同时只更新数据?
我需要创作一部电影。假设我创建了一个轴,并在其上绘制了一些非常定制的内容:在Matlab axis中,如何在保持所有轴属性的同时只更新数据?,matlab,matlab-figure,axis-labels,Matlab,Matlab Figure,Axis Labels,我需要创作一部电影。假设我创建了一个轴,并在其上绘制了一些非常定制的内容: figure; ax = plot(x, y, 'linewidth', 3, 'prop1', value1, 'prop2', value2, ...); grid minor; axis(ax, [xmin xmax ymin ymax]); legend(ax, ...); xlabel(ax, ...); ylabel(ax, ...); title(ax, ...); 现在我运行一个循环,其中只更新y的值
figure;
ax = plot(x, y, 'linewidth', 3, 'prop1', value1, 'prop2', value2, ...);
grid minor;
axis(ax, [xmin xmax ymin ymax]);
legend(ax, ...);
xlabel(ax, ...);
ylabel(ax, ...);
title(ax, ...);
现在我运行一个循环,其中只更新y
的值
for k = 1 : N
% y changes, update the axis
end
使用新的
y
(或x
和y
)更新轴以保留所有轴属性的最快和最简单的方法是什么?只需将轴句柄传递回后续的打印命令即可
i、 e
而不是
ax = plot(...)
一种快速方法是简单地更新所绘制数据的y值:
%# note: plot returns the handle to the line, not the axes
%# ax = gca returns the handle to the axes
lineHandle = plot(x, y, 'linewidth', 3, 'prop1', value1, 'prop2', value2, ...);
%# in the loop
set(lineHandle,'ydata',newYdata)
编辑如果有多行,即lineHandle
是向量,该怎么办?您仍然可以在一个步骤中更新;不过,您需要将数据转换为单元格数组
%# make a plot with random data
lineHandle = plot(rand(12));
%# create new data
newYdata = randn(12);
newYcell = mat2cell(newYdata,12,ones(1,12));
%# set new y-data. Make sure that there is a row in
%# newYcell for each element in lineH (i.e. that it is a n-by-1 vector
set(lineHandle,{'ydata'},newYcell(:) );
抱歉,learnvst,但它不起作用。绘图(ax…)删除所有轴properties@Serg:如果在下一个绘图命令之前调用
hold all
,您将保留旧轴属性,但也保留旧线。@乔纳斯:我知道,但我不想保留旧线。谢谢,乔纳斯。它起作用了。顺便说一句,如果y是一个矩阵,那么lineHandle是一个向量,所以我为每一列调用set(lineHandle(I),'ydata',newYdata(:,I)),对吗?或者有一个技巧可以一次更新y的所有列?
%# make a plot with random data
lineHandle = plot(rand(12));
%# create new data
newYdata = randn(12);
newYcell = mat2cell(newYdata,12,ones(1,12));
%# set new y-data. Make sure that there is a row in
%# newYcell for each element in lineH (i.e. that it is a n-by-1 vector
set(lineHandle,{'ydata'},newYcell(:) );