How to write a short syntax for the legend?

1 view (last 30 days)
Wiqas Ahmad
Wiqas Ahmad on 4 Mar 2021
Commented: Wiqas Ahmad on 4 Mar 2021
The legend in the plot shows seven lines is according to the length of EC in the program. I want to write one syntax for the legend in terms of the index i instead of seven, so that it repeats for the values of EC and gives me the same figure
clear all
EC = [0.0052 0.0078 0.0104 0.013 0.0156 0.0182 0.0208];
Reff = 7;%if we write Ref=4:1:20 then all the effective radii from 1 to 20 will be executed at once and will gives 20 figures, otherwise writing only one will execute only one
for j = 1:length(Reff)
hold on
for i = 1:length(EC)
filename1 = [num2str(EC(i)),'\',num2str(Reff(j)),'um\out_resultsG_I0.dat'];
I1 = getsignal(filename1);
I0_1 = sum(I1,2);
filename1 = [num2str(EC(i)),'\',num2str(Reff(j)),'um\out_resultsG_Q0.dat'];
Q1 = getsignal(filename1);
Q0_1 = sum(Q1,2);
hold off
title(['Effective radius=',num2str(Reff(j)),'\mum',', FOV=2mrad'],'FontSize',12,'FontWeight','normal');
xlabel('Depolarisation ratio \delta_{out}','FontSize',12,'FontWeight','normal');
ylabel('Cloud depth (m)','FontSize',12,'FontWeight','normal');

Answers (1)

Steven Lord
Steven Lord on 4 Mar 2021
Set the DisplayName property of each line just like you set the LineWidth. Then legend show will make the legend appear and use the DisplayName property values.
x = 0:360;
axis([0 360 -1 1])
hold on
plot(x, sind(x), 'ro-', 'MarkerIndices', 1:60:numel(x), 'DisplayName', 'sine')
plot(x, cosd(x), 'k^:', 'MarkerIndices', 30:60:numel(x), 'DisplayName', 'cosine')
legend show
I used MarkerIndices too because I didn't want the plots to be covered with their markers.
  1 Comment
Wiqas Ahmad
Wiqas Ahmad on 4 Mar 2021
I think this is not my question. Because if I want to plot seven lines, so I have to write seven syntaxes which I have already done it in the legend. My question is that If you see the syntax ['EC=',num2str(EC(1))in the legend which I have repeated for each line display in the legend. what I want is to write one syntax only with introducing index i so that it repeates seven times according to length of EC and give me the same plot.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!