legend for hold function

5 views (last 30 days)
Josefina Ottitsch
Josefina Ottitsch on 27 Feb 2019
Commented: Josefina Ottitsch on 27 Feb 2019
Hello,
I have a problem with a function I am using in a script.
in my script I am letting my program run through 1 file. The file consists of around 200 different but similar measurements, each measurement consists of around 500 x and y values.
in my script I tell my program to only plot lets say every 30th measurement. Therefore, my desired function plots around 200/30 = 6 graphs. I am using hold, so these 12 graphs are in one diagram.
My problem: I have difficulties setting up my legend.
currently the program looks like this:
for e=1:6
ee(e)=30*e
for i=1:length(ee)
legendstr{i}=num2str(ee(i));
legend('DisplayName','Measurement',legendstr)
hold all
end
end
while using this method, my legend only consists of the numbers 30 60 90 120 150 180 written downwards. But I cannot manage to write Measurement 30 Measurement 60 ... and so on
Does anyone know how to do this?
Thank you for the support!

Accepted Answer

Alex Mcaulley
Alex Mcaulley on 27 Feb 2019
I think there is no 'DisplayName' property for a legend, it is aproperty of a chart, but you can do the same as you want just putting
for e=1:6
ee(e)=30*e
for i=1:length(ee)
legendstr{i}=['Measurement' num2str(ee(i))];
legend(legendstr)
hold all
end
end
Another option is setting in your plots the 'DisplayName' property. For example:
for e=1:6
ee(e)=30*e
plot(x,y,'DisplayName',['Measurement' num2str(ee(e))])
hold on
end
legend('show')
  1 Comment
Josefina Ottitsch
Josefina Ottitsch on 27 Feb 2019
Thank you so much! I used the first answer and put a comma between measurement and num2str. It works now! Thank you!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!