MATLAB中的动态图正在改变我的刻度标签

时间:2014-08-03 09:45:11

标签: matlab matlab-figure

嗨所以我试图绘制一个动态条形图,其中Y轴刻度标记从-90到90.当我在非动态条形图上执行此操作时,刻度标签会正确显示。一旦我让它充满活力,他们就到处都是。

希望有人能看到我出错的地方..

% Generate a random vector of -90 to +90 to simulate pitch.

pitch = 90*rand(1) - 90*rand(1);

%Static bar graph, y tick marks are working here
subplot(2,1,1);
bar(pitch);
set(gca,'YLim',[-90.5 90.5]);
set(gca,'YTick',[-90,-45,0,45,90]);
title('Pitch');

%Dynamic bar graph, y tick marks are not working here
subplot(2,1,2);
barGraph = bar(pitch);
set(gca,'YLim',[-90.5 90.5]);
set(gca,'YTick',[-90,-45,0,45,90]);
title('Pitch');


 for j=1:1000,
     pitch = 90*rand(1) - 90*rand(1);
     set(barGraph,'YData',pitch);
     drawnow;
     pause(0.1);
 end

2 个答案:

答案 0 :(得分:2)

方法1

set(gca....之后和set(....'YData'....)之前将2 drawnow行移动到循环中。

for jj=1:20
    pitch = 90*rand(1) - 90*rand(1);
    set(barGraph,'YData',pitch);
    set(gca,'YLim',[-90.5 90.5]);
    set(gca,'YTick',[-90,-45,0,45,90]);
    drawnow;
    pause(0.1);
end

方法2

正如@ Dev-iL建议的那样,使用hold on来保持轴属性通过循环。 但是使用hold on只会产生一个问题,如果你运行脚本超过1次,条形图将相互叠加。一起使用hold off将解决此问题。

hold on
for jj=1:20
    pitch = 90*rand(1) - 90*rand(1);
    set(barGraph,'YData',pitch);
    drawnow;
    pause(0.1);
end
hold off

方法3(不好)

在循环之前,将属性YDataSource设置为'pitch'。在循环内部,使用refreshdata更新图表。 还需要hold onhold off

set(gca,'YLim',[-90.5 90.5]);
set(gca,'YTick',[-90,-45,0,45,90]);
set(barGraph, 'YDataSource', 'pitch');
hold on

for jj=1:20
    pitch = 90*rand(1) - 90*rand(1);
    refreshdata;
    pause(0.1);
end
hold off

此方法的性能比前者慢2.5倍。不要使用它。

关于循环迭代器j

j以及i是许多流行语言中最常用的循环迭代器之一。但是在Matlab中,它们是专门分配给想象单元的。可以覆盖此特殊变量的默认值,但it is not good

改为使用iijj

答案 1 :(得分:1)

只需在循环前添加hold on即可。 (我正在使用MATLAB 2014a)