Matlab 在绘图上插入间隔标签

Matlab 在绘图上插入间隔标签,matlab,plot,labels,Matlab,Plot,Labels,我有一个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') 我想在绘图7标签上显示间隔(w1,w2,w3,w4,

我有一个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')
我想在绘图7标签上显示间隔(w1,w2,w3,w4,w5,w6,w7)。
谢谢

假设您想看到图例上的不同线条,我建议您分别绘制它们。下面是一个小规模的例子:

plot(1:100,1:100,101:200,101:200)
legend('a','b')
像这样的

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)
尝试使用and(双箭头)指示您的间隔。但是请注意,出于某种原因,
注释
使用标准化地物单位指定注释的
x
y
坐标,因此您可能希望使用文件交换中的此非常有用的实用函数将坐标从数据空间转换为标准化地物单位:

编辑: 因为我觉得自己很慷慨,所以可能看起来是这样的:

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

您希望标签显示在哪里?在图表上?在axis上?我也在matlab网站上发布过,你可以看到绘图,我不是在寻找图例,我想插入标签,指示绘图附近的间隔。@user3209872你可以创建一个示例(使用油漆或其他方法)并将其添加到问题中吗?抱歉,丹尼斯,我没有足够的理由在Stackoverflow上添加一个数字。我有我的间隔点(I),我想要在绘图上标记间隔(例如从1:1430)。我尝试使用文本插入或clabel。它不起作用。运行我的文件,您可以看到间隔。Thankstory,未运行,无标题(第10行)[xa,ya]=ds2nfu([t(I(k)),t(I(k+1))],[2.5 2.5]中出现错误;您是否已下载了我在回答中建议的实用功能“数据空间到图形单位转换”?就是这样。您需要下载它并将其放置在MATLAB路径上。