2012-03-02 61 views
16

我打开了多个数字,我想在运行时独立更新它们。下面玩具的例子应该澄清我的意图:我该如何指定一个情节应该去哪个图?

clf; 

figure('name', 'a and b'); % a and b should be plotted to this window 
hold on; 
ylim([-100, 100]); 

figure('name', 'c'); % only c should be plotted to this window 

a = 0; 
b = []; 
for i = 1:100 
    a = a + 1; 
    b = [b, -i]; 
    c = b; 
    xlim([0, i]); 
    plot(i, a, 'o'); 
    plot(i, b(i), '.r'); 
    drawnow; 
end 

这里的问题是,当我打开第二个figure,我不能告诉plot功能绘制的第一个,而不是第二个(只c应绘制到第二个)。

回答

17

您可以使用类似

figure(1) 
plot(x,y) % this will go on figure 1 

figure(2) 
plot(z,w) % this will go on another figure 

该命令也将设置在图中可以看到,并在一切之上。

可以来回切换通过发出相同figure命令作为必要的数字之间。或者,您可以用手柄的人物和:

h=figure(...) 

,然后发出figure(h)而不是使用数字索引。有了这个语法,你也可以通过使用

set(0,'CurrentFigure',h) 
13

您可以指定在剧情命令轴对象防止图上顶部弹出。在这里看到:

http://www.mathworks.de/help/techdoc/ref/plot.html

所以,开一个人物,插入轴,保存轴的ID对象,然后绘制到它:

figure 
hAx1 = axes; 
plot(hAx1, 1, 1, '*r') 
hold on 

figure 
hAx2 = axes; 
plot(hAx2, 2, 1, '*r') 
hold on 


plot(hAx2, 3, 4, '*b') 
plot(hAx1, 3, 3, '*b') 

另外,您可以使用gca而不是创建对象轴自己(因为它的实际数字中自动创建,当它不存在!)

figure 
plot(1,1) 
hAx1 = gca; 
hold on 

figure 
plot(2,2) 

plot(hAx1, 3, 3) 

参见附图表示之间的关系下面的层级和轴

enter image description here

http://www.mathworks.de/help/techdoc/learn_matlab/f3-15974.html

+1

但是,为什么操纵轴?如果我根本不想要轴,该怎么办?这对我来说似乎有些复杂(新的Matlab)。你能解释一下吗? – 2012-03-02 13:53:47

+3

因为有事,你总是绘制进入一个轴对象(你不能没有轴情节);)当你不使用'axes'命令,'plot'时候自动创建它们的数字并不包含这些内容。所以这是正确的路要走。看到我编辑的帖子! – tim 2012-03-02 14:06:43

+1

谢谢,但我仍然想知道,为什么'plot'命令使用轴柄而不是图柄 - 它看起来更直观。 – 2012-03-02 14:20:58