Note: The text appears in stupid places in the code above, because the data are random, but the positions make sense in my code. I am just interested to know how to print one label per plot!
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 个评论
采纳的回答
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 Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!