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
clc
clear all
%------------------------------Program-------------------------------------
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
h=2115:9:2394;
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);
I1(isnan(I1))=0;
I0_1 = sum(I1,2);
filename1 = [num2str(EC(i)),'\',num2str(Reff(j)),'um\out_resultsG_Q0.dat'];
Q1 = getsignal(filename1);
Q1(isnan(Q1))=0;
Q0_1 = sum(Q1,2);
dep(:,j)=(I0_1-Q0_1)./(I0_1+Q0_1);
plot(dep,h,'LineWidth',2);
end
end
%--------------------------------------------------------------------------
hold off
%-----------------------------Title----------------------------------------
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');
%------------------------------Legend--------------------------------------
legend(['EC=',num2str(EC(1)),'/m'],['EC=',num2str(EC(2)),'/m'],['EC=',num2str(EC(3)),'/m'],...
['EC=',num2str(EC(4)),'/m'],['EC=',num2str(EC(5)),'/m'],['EC=',num2str(EC(6)),'/m'],...
['EC=',num2str(EC(7)),'/m'],'location','Southeast')
.

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.

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!