2015-09-25 164 views
0

我有4個不同的數字。每個圖包含2個子圖(2行1列)合併4個子圖4個子圖

可以使用以下代碼生成數字。

y = [2 2 3; 2 5 6; 2 8 9; 2 11 12]; 

for i = 1 : 4 
    figure(i) 
    subplot(2,1,1) 
    bar(y) 
    subplot(2,1,2) 
    bar(y) 
end 

有了這4位數字,是否有可能他們1個圖結合?

提供的解決方案不適用於其他示例,其中我使用barwitherr創建圖形,爲什麼?

for i = 1 : 4 
    figure(i) 
    subplot(2,1,1) 
    barwitherr([1 2 3 4;1 2 1 2], [5 6 7 8;1 2 3 4]) 
    subplot(2,1,2) 
    barwitherr([1 2 3 4;1 2 1 2], [5 6 7 8;1 2 3 4]) 
end 
for i = 1:4 
    figure(i); 
    ax = gca; 
    f = get(ax, 'children'); 

    figure(5); 
    s = subplot(2, 2, i); 
    copyobj(f, s); 
end 
+0

在創建或之後合併? – thewaywewalk

+1

我很困惑,這裏有8個子圖。 – excaza

+0

後合併 – gabboshow

回答

0

這可能不是你想要的,但是非常具有可擴展性。您可以遍歷每個原始4位數字,並獲取其中的每個subplot s的句柄。一旦圖中我們有興趣使用figure(i)是當前gcf對象,我們可以得到一個處理每一個插曲元素與s = subplot(2, 1, i)提供我們所知道的次要情節的結構和i是次要情節,我們感興趣的

我們然後我們用copyobj()allchild()超過每個次要情節

copyobj(allchild(h), s) 

allchild()副本在所有的信息複製到一個新的次要情節,在新圖中barwitherr()被冷落,從你從複製的代碼以前編輯我對你問題的回答。

如果我們把所有這一切,我們可以一起產生全部代碼

for i = 1:4 
    figure(5); 

    n = i + (i - 1); 

    s1 = subplot(4, 2, n); 
    s2 = subplot(4, 2, n+1); 

    h = figure(i); 
    hs1 = subplot(2, 1, 1); 
    hs2 = subplot(2, 1, 2); 

    copyobj(allchild(hs1), s1) 
    copyobj(allchild(hs2), s2) 
end 

n = i + (i - 1);用於複製原始排序。由該產生的輸出是

                Output

+0

嗨,我已經有4個數字有2個子圖,我想合併4個子圖中的4個數字有4個子圖 – gabboshow

+0

@IKavanagh他不需要幫助正確創建圖,正如你的答案解釋,他需要幫助合併已有的地塊。 – thewaywewalk

+1

@thewaywewalk因此,他想要分別拍攝4個獨立的圖和2個子圖,並將它們合併爲4個子圖的1個圖形? – IKavanagh