Add text to plot using a loop

6 次查看(过去 30 天)
I am plotting FTIR data. The code below should work, but I have substituted in randomly generated data.
I'd like the text function in the loop to produce a label for each plot. i.e., p1(1) will be labelled pre-sorption, and p1(5) will be labelled 'SiO2 = 2 mM'. Currently, the code produces text in the right position, but all five labels appear at all five locations. How do I modify the code to make sure each label appears sequentially?
Thanks!
AM_cm=([400:1:4000]);
AM_cm=AM_cm.';
AM=([rand(3601, 1) rand(3601, 1) rand(3601, 1) rand(3601, 1) rand(3601, 1)]);
figure(1);
set(gca,'Xdir','reverse');
set(gca,'ytick',[]); set(gca,'yticklabel',[]);
xlabel ('cm-1','Fontsize', 14); ylabel ('Absorbance','Fontsize', 14);
for a = 1:5% FTIR data
hold on; box on;
p1(a)=plot(AM_cm,AM(:,a),'-','LineWidth',2);
text(3900, AM(1,a),['pre-sorption','SiO2= 0.5 mM','SiO2 = 1 mM','SiO2 = 1.5 mM','SiO2 = 2 mM'],'Fontsize', 12);
end
  3 个评论
Rosalie Tostevin
Rosalie Tostevin 2023-8-22
I'd rather label the lines than have a legend, because they are all the same colour.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2023-8-22
AM_cm=([400:1:4000]);
AM_cm=AM_cm.';
AM=([rand(3601, 1) rand(3601, 1) rand(3601, 1) rand(3601, 1) rand(3601, 1)]);
figure(1);
set(gca,'Xdir','reverse');
set(gca,'ytick',[]); set(gca,'yticklabel',[]);
xlabel ('cm-1','Fontsize', 14); ylabel ('Absorbance','Fontsize', 14);
labels = {'pre-sorption','SiO2= 0.5 mM','SiO2 = 1 mM','SiO2 = 1.5 mM','SiO2 = 2 mM'};
for a = 1:length(labels)% FTIR data
hold on; box on;
p1(a)=plot(AM_cm,AM(:,a),'-','LineWidth',2);
text(3900, AM(1,a),labels{a},'Fontsize', 12);
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by