Add text to plot using a loop

6 vues (au cours des 30 derniers jours)
Rosalie Tostevin
Rosalie Tostevin le 22 Août 2023
Commenté : Rosalie Tostevin le 22 Août 2023
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 commentaires
Dyuman Joshi
Dyuman Joshi le 22 Août 2023
Do you want a legend?
Rosalie Tostevin
Rosalie Tostevin le 22 Août 2023
I'd rather label the lines than have a legend, because they are all the same colour.

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 22 Août 2023
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
  1 commentaire
Rosalie Tostevin
Rosalie Tostevin le 22 Août 2023
This works, thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by