2014-01-29 45 views
0

我有7個間隔的曲線圖:在積7個標籤插入間隔的標籤上情節

t=0:0.001:10; 
y_fcn = @(t) 0.2*cos(t) + cos(1.4*t) + 0.8*cos(5.2*t) + 0.02*randn(1, length(t)); 
plot(t, y_fcn(t), '-b'); 
hold on 
I = [1, 1430, 2859, 4288, 5717, 7146, 8575, 10001]; 
plot(t(I), y_fcn(t(I)), '*r') 

我想指示所述區間爲(W1,W2,W3,W4,W5,W6,W7)。 謝謝

+0

你想去哪裏標籤出現?在圖上?在一個軸上? – Dan

+0

是的圖上, – user3209872

+0

我也發佈在MATLAB網站上,你可以看到情節http://www.mathworks.co.uk/matlabcentral/answers/114150-how-create-lines-on-plot – user3209872

回答

0

嘗試使用textannotation(雙箭頭),表示你的時間間隔。但請注意,由於某些原因,annotation使用標準化的圖形單位來指定註釋的xy座標,所以您可能想要使用File Exchange中非常有用的實用程序函數將座標從數據空間轉換爲標準化的圖形單位: Data space to figure units conversion

編輯: 因爲我感覺大方,它可能看起來像:

t=0:0.001:10; 
y_fcn = @(t) 0.2*cos(t) + cos(1.4*t) + 0.8*cos(5.2*t) + 0.02*randn(1, length(t)); 
plot(t, y_fcn(t), '-b'); 
axis([min(t) max(t) -2 3]); 
hold on 
I = [1, 1430, 2859, 4288, 5717, 7146, 8575, 10001]; 
plot(t(I), y_fcn(t(I)), '*r') 

for k=1:length(I)-1 
    [xa,ya] = ds2nfu([t(I(k)) t(I(k+1))],[2.5 2.5]); 
    annotation('doublearrow',xa,ya,'Color','r') 
    y_lim = get(gca,'ylim'); 
    line([t(I(k)) t(I(k))],y_lim,'Color','r','LineStyle',':') 
    text(0.5*(t(I(k))+t(I(k+1))),2.7,['w' num2str(k)],'Color','r') 
end 
+0

對不起,沒有運行,錯誤在無標題(第10行) [xa,ya] = ds2nfu([t(I(k)),t(I(k + 1))],[2.5 2.5]) ; – user3209872

+0

您是否按照我在答案中的建議下載了實用程序功能「數據空間到圖單位轉換」?這就是這個。你需要下載它並將它放在你的MATLAB路徑上。 – am304

+0

這是工作,謝謝 – user3209872

0

假設你想看到圖例上的不同的行,我會建議分開繪製它們。這裏有一個小例子:

plot(1:100,1:100,101:200,101:200) 
legend('a','b') 
+0

我不是尋找傳說,我想插入指示間隔接近情節的標籤。 – user3209872

+0

@ user3209872你可以創建一個例子(使用paint等)並將其添加到問題中嗎? –

+0

對不起丹尼斯,我沒有足夠的觀點把數字放在Stackoverflow上。我有我的間隔點(I),我想標籤間隔(例如從1:1430)。我試圖使用文本插入或clabel。它不工作。運行我的文件,你可以看到間隔。 謝謝 – user3209872

1

是這樣的嗎?

strings = {'w1';'w2';'w3';'w4';'w5';'w6';'w7'}; 
x_strings = (t(I(1:7))+t(I(2:8)))/2; %// center of each interval 
y_strings = y_fcn(x_strings) + .9; %// height from y_fcn. Adjust ".9" as needed 
text(x_strings,y_strings,strings)