matlab - 在 Matlab 轴中,如何在保持所有轴属性的同时只更新数据?

标签 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 的值被更新。

for k = 1 : N
% y changes, update the axis
end

用新的 y(或 xy)更新轴的最快和最简单的方法是什么,同时保留所有轴属性?

最佳答案

一种快速的方法是简单地更新您绘制的数据的 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(:) );

关于matlab - 在 Matlab 轴中,如何在保持所有轴属性的同时只更新数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10317423/

相关文章:

matlab - Matlab中的指示矩阵

matlab - 什么时候需要 eval 函数?

matlab - 无需 for 循环即可更改条形图中的单个条形颜色

Matlab:格式化轮廓标签中的小数

matlab - 绘制隐藏变量/参数

r - 在ggplot2中向x轴添加文本

datetime - 时间增量的绘图轴,格式为 DD HH :MM:SS with matplotlib

matlab - 如何压制数字?

arrays - Matlab 中的自定义排序数组

Highcharts yaxis 标签格式