how can I show all of my data in the legend for each curve?

My plot has 10 curves which are according to the data of EC as shown in figure. However the legend displays only the last term in EC, where EC is a row vector as shown. I want to display all the data terms in EC in the legend one by one to exactly express the plotting curves. My syntax for the legend is given by:
EC = [0.0052 0.0078 0.0104 0.0130 0.0156 0.0182 0.0208 0.0234 0.0260 0.0286];
for j = 1:length(EC)
.
.
.
end
lgd=legend(['EC=',num2str(EC(j)),'\m'])

Answers (2)

lgd is outside the for loop with no buffer array to hold the data. This is why only the last value is being displayed. While this would work if the position of lgd was changed to inside the for loop any inclusion of a for loop with the plot command makes me a little nervous due to the computational resorces required.
I would suggest trying something like this instead: (note the changes to 'legend')
EC = [0.0052 0.0078 0.0104 0.0130 0.0156 0.0182 0.0208 0.0234 0.0260 0.0286];
legend(['EC=',num2str(EC),'\m']);
There may need to be some additional formatting however that should display everything. Let the forumm know how it goes and if you need any other help with it.
Christopher
Try this
[x,y] = meshgrid(0:0.1:5,0:5);
y = sin(x)./y;
plot(x',y')
s0 = sprintf('curve #%d,',0:5);
s1 = strsplit(s0,',');
legend(s1{1:end-1})

3 Comments

None of them works. I have tried another legend syntax that's working well but I have to repeat the string 10 times for 10 values of EC as shown. And what I want is that to write one legend syntax so that it repeats for every value of EC
lgd=legend(['EC=',num2str(EC(1)),'m^{-1}'],['EC=',num2str(EC(2)),'m^{-1}'],['EC=',num2str(EC(3)),'m^{-1}'],...%dashes can be used to strt new line
['EC=',num2str(EC(4)),'m^{-1}'],['EC=',num2str(EC(5)),'m^{-1}'],['EC=',num2str(EC(6)),'m^{-1}'],...
['EC=',num2str(EC(7)),'m^{-1}'],['EC=',num2str(EC(8)),'m^{-1}'],['EC=',num2str(EC(9)),'m^{-1}'],...
['EC=',num2str(EC(10)),'m^{-1}'])
  • None of them works.
Its impossible. Can you show the code?
clc;
clear all;
%------------------------------Program-------------------------------------
z=[2115:9:2394];
z=z';% dash is used for transpose
FOV=2;
EC = [0.0052 0.0078 0.0104 0.0130 0.0156 0.0182 0.0208 0.0234 0.0260 0.0286];%%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
Reff =[4:1:20];
for i=1:length(FOV)
figure(1),
hold on
for j = 1:length(EC)
for k = 1:length(Reff)
filename1 = ['FOV_',num2str(FOV(i)),'mrad\',num2str(EC(j)),'\',num2str(Reff(k)),'um\out_resultsG_I0.dat'];
eval(['I',num2str(i),num2str(j),num2str(k),'=','getsiganl(filename1)',';']);
eval(['II',num2str(i),num2str(j),num2str(k),'=','smooth(sum(I',num2str(i),num2str(j),num2str(k),',2)',');']);
filename2 = ['FOV_',num2str(FOV(i)),'mrad\',num2str(EC(j)),'\',num2str(Reff(k)),'um\out_resultsG_Q0.dat'];
eval(['Q',num2str(i),num2str(j),num2str(k),'=','getsiganl(filename2)',';']);
eval(['QQ',num2str(i),num2str(j),num2str(k),'=','smooth(sum(Q',num2str(i),num2str(j),num2str(k),',2)',');']);
eval(['dep',num2str(i),num2str(j),num2str(k),'=','(II',num2str(i),num2str(j),num2str(k),'-QQ',num2str(i),num2str(j),num2str(k),')./(II',num2str(i),num2str(j),num2str(k),'+QQ',num2str(i),num2str(j),num2str(k),')',';']);
eval(['depi',num2str(i),num2str(j),num2str(k),'=','cumtrapz(II',num2str(i),num2str(j),num2str(k),'-QQ',num2str(i),num2str(j),num2str(k),')./cumtrapz(II',num2str(i),num2str(j),num2str(k),'+QQ',num2str(i),num2str(j),num2str(k),')',';']);
% eval(['values = spcrv([[dep',num2str(i),num2str(j),num2str(k),'(1);dep',num2str(i),num2str(j),num2str(k),';dep',num2str(i),num2str(j),num2str(k),'(end)] [z(1);z;z(end)]]'',6)',';']);
% plot(values(1,:),values(2,:));
eval(['plot(dep',num2str(i),num2str(j),num2str(k),',z)',';']);
title(['FOV=2mrad'],'FontSize',16,'FontWeight','normal')%\bf(bold font),\rm(normal font),\it(italian font)
xlabel('\fontname{Arial}Depolarisation ratio \delta_{out}','FontSize',16,'FontWeight','normal');
ylabel('\fontname{Arial}Cloud depth (m)','FontSize',16,'FontWeight','normal');
end
end
end
set(gca,'xlim',[0 1]);
set(gca,'ylim',[2100 2410]);
set(gca,'fontsize',14);
box on;
grid on;
lgd=legend(['EC=',num2str(EC(1)),'m^{-1}'],['EC=',num2str(EC(2)),'m^{-1}'],['EC=',num2str(EC(3)),'m^{-1}'],...%dashes can be used to strt new line
['EC=',num2str(EC(4)),'m^{-1}'],['EC=',num2str(EC(5)),'m^{-1}'],['EC=',num2str(EC(6)),'m^{-1}'],...
['EC=',num2str(EC(7)),'m^{-1}'],['EC=',num2str(EC(8)),'m^{-1}'],['EC=',num2str(EC(9)),'m^{-1}'],...
['EC=',num2str(EC(10)),'m^{-1}']);
lgd=legend('location','Southeast');
lgd=legend('show','boxon');%hide%boxoff
%position: northwest,northeast,.....
lgd.FontSize = 10;
lgd.FontWeight = 'normal';%bold
lgd.Orientation='vertical';%horizantal
lgd.NumColumns = 1;
%lgd.Title.String = 'My Legend Title';
%lgd.Title.FontSize = 8;
figure(2)
hold on
plot(dep111,z,'ob-');
plot(dep112,z,'+b-');
plot(dep113,z,'*b-');
plot(dep114,z,'<b-');
plot(dep115,z,'>b-');
plot(dep116,z,'sb-');
plot(dep117,z,'hb-');
plot(dep118,z,'pb-');
plot(dep119,z,'db-');
plot(dep1110,z,'b-','Marker','<','MarkerFaceColor','b');
plot(dep1111,z,'b-','Marker','o','MarkerFaceColor','b');
plot(dep1112,z,'b-','Marker','s','MarkerFaceColor','b');
plot(dep1113,z,'b-','Marker','h','MarkerFaceColor','b');
plot(dep1114,z,'b-','Marker','p','MarkerFaceColor','b');
plot(dep1115,z,'b-','Marker','d','MarkerFaceColor','b');
plot(dep1116,z,'b-','Marker','>','MarkerFaceColor','b');
plot(dep1117,z,'b-','Marker','^','MarkerFaceColor','b');
xlabel('\delta_{out}');
ylabel('Cloud depth(m)');
set(gca,'xlim',[0 0.12]);
set(gca,'ylim',[2100 2410]);
set(gca,'fontsize',14);
box on;
grid on;
lgd=legend([compose('R_{eff} %d\\mum',(4:20))])
lgd=legend('show','location','Northwest','boxon')%hide%boxoff
%position: northwest,northeast,.....
lgd.FontSize = 8;
lgd.FontWeight = 'normal'%bold
lgd.Orientation='vertical'%horizantle
lgd.NumColumns = 1;
lgd=legend('location','Northwest')
lgd=legend('show','boxon')%hide%boxoff
%position: northwest,northeast,.....
lgd.FontSize = 10;
lgd.FontWeight = 'normal'%bold
lgd.Orientation='vertical'%horizantal
lgd.NumColumns = 1;
%lgd.Title.String = 'My Legend Title';
%lgd.Title.FontSize = 8;

Sign in to comment.

Asked:

on 20 Mar 2021

Commented:

on 22 Mar 2021

Community Treasure Hunt

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

Start Hunting!