嗨所以我试图绘制一个动态条形图,其中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
答案 0 :(得分:2)
在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
正如@ 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
在循环之前,将属性YDataSource
设置为'pitch'
。在循环内部,使用refreshdata
更新图表。
还需要hold on
和hold 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。
改为使用ii
和jj
。
答案 1 :(得分:1)
只需在循环前添加hold on
即可。 (我正在使用MATLAB 2014a)