Adding legend in a plot genereted by a loop

Hello everyone
i have a lot of x,y data sets that i would like to plot. The data sets are stored in a cell structure so that
  • DataFiles(1,1) is the first data set i imported
  • DataFiles(1,2) is the second
  • DataFiles(1,1).data(:,2) is all the y values of the first data set
and so on. the names of the data sets are furthermore stored in OutFiles so that
  • OutFiles(1,1).name is the name of the first data set
  • OutFiles(2,1).name is the name of the second data set
and so on. because i would like to plot all the x,y data set togehter i wrote a loop. the code is shown below
figure( 'Name', 'Reduced data' );
title('Reduced Raw data')
cc = jet(length(OutFiles)); % creates colormap
% log-lin plot
subplot(2,1,1)
h1 = subplot(2,1,1);
set(h1, 'xscale', 'lin')
set(h1, 'yscale', 'log')
xlabel('Q (Å^{-1})','fontsize',14)
ylabel('Detector counts in 1 sec.')
hold on
for k = 1:length(OutFiles)
plot(DataFiles{1,k}.data(:,1),DataFiles{1,k}.data(:,2), 'color', cc(k,:));
end
+ the other subplot which is just a log-log plot of the same
My problem is that i cannot figure out how i can include the legends in the plot. i have tried include "legend" in the loop so that the loop code becomes
for k = 1:length(OutFiles)
plot(DataFiles{1,k}.data(:,1),DataFiles{1,k}.data(:,2), 'color', cc(k,:));
legend(OutFiles(k,1)
end
but this does not work. Simply adding legend(OutFiles) also does not work but OutFiles also contains more information than ".name". i thought about making an array of the names "Legend(1,length(OutFiles))" but i do not know how to make an array of strings. i tried using
>> Legend=zeros(1,10); >> Legend(1,8) = 'test' Subscripted assignment dimension mismatch.
but as you can see i cannot add a string to an array
does anyone have a suggestion on how to add legends to plots made using a loop? any help will be greatly appreciated :)

1 件のコメント

mazari ahmed
mazari ahmed 2015 年 8 月 12 日
l want to make a legend for my figure but l have 3 for loops with three plots like mentioned below : legend 1 : essential legend 2 : less essential legend 3 : most important
for P=1:K
% body
plot(.....,'bo');
end
for Z=N:T
%body
plot(....,'ro'):
end
for i=1:L
%body
plot(....,'ko'):
end
how can l manage my legend ? thank you

サインインしてコメントする。

 採用された回答

Youssef  Khmou
Youssef Khmou 2013 年 2 月 6 日

18 投票

hi, you plot all the data first, and then manipulate the Legend using per example cells as shown below :
for k = 1:length(OutFiles)
plot(DataFiles{1,k}.data(:,1),DataFiles{1,k}.data(:,2), 'color', cc(k,:));
end
Legend=cell(2,1)% two positions
Legend{1}=' Your data 1' ;
Legend{2}=' Your data 2';
legend(Legend);
In case there are many legends, like N plots, then you do :
Legend=cell(N,1)
for iter=1:N
Legend{iter}=strcat('Your_Data number', num2str(iter));
end
legend(Legend)

8 件のコメント

Martin Nors Pedersen
Martin Nors Pedersen 2013 年 2 月 6 日
編集済み: Martin Nors Pedersen 2013 年 2 月 6 日
Thank you very much! your solution works and plots the file name when i use
Legend=cell(length(OutFiles),1);
for p=1:length(OutFiles)
Legend{p}=strcat(OutFiles(p,1).name);
end
it does however give me the warnings messages
Warning: Negative data ignored
> In is2D at 12
In legend>make_legend at 333
In legend at 284
In SAXSanalysis at 55
Warning: Negative data ignored
> In scribe\private\get_legendable_children at 14
In graph2dhelper at 31
In legend>make_legend at 347
In legend at 284
In SAXSanalysis at 55
Warning: Negative data ignored
> In legendcolorbarlayout>doParentResize at 335
In SAXSanalysis at 59
Warning: Negative data ignored
> In legendcolorbarlayout>doParentResize at 335
In SAXSanalysis at 59
the negatives y-values is a problem because i use log scale for the y-values. this is a separate problem i am working on. i was given these warnings before i emplemented the code for legends
but i cannot understand the other messages i am given. they started to appear when i introduced the code shown above. i can see there is some problem but the warnings do not state a specific problem - they just mention several lines for different matlab functions
Youssef  Khmou
Youssef Khmou 2013 年 2 月 6 日
hi,Martin, good that it worked, but i have no idea about the warning, in the mean time i am sure there is command like " warning off" maybe , it can be used to hide the message .
Ankit Singh
Ankit Singh 2017 年 3 月 31 日
編集済み: Ankit Singh 2017 年 3 月 31 日
Thank you for you help, i am able to use it in a simple script file, but if i try to apply the same in gui i get an error saying "invalid argument" please help. I used the following mentioned code.
Legend=cell(length(OutFiles),1);
for p=1:length(OutFiles)
Legend{p}=strcat(OutFiles(p,1).name);
end
Aram Kirakosyan
Aram Kirakosyan 2017 年 8 月 17 日
Thanks for the solution.
Nathaniel H Werner
Nathaniel H Werner 2020 年 1 月 4 日
Is there a way this can be done to skip some entries in the loop? I am making a complicated plot, but only a portion of what is plotted represented actual data. The rest is just for visual aid.
Carlos Astudillo Gutierrez
Carlos Astudillo Gutierrez 2020 年 3 月 23 日
Youssef Khmou! Thanks. You answer worked for me very well!
Shivam Chopra
Shivam Chopra 2020 年 6 月 1 日
Hi, can you help me in my case??
Thanks in advance
p = (1);
B = [1E-9 1E-8 1E-7 1E-6 1E-5 1E-4 1E-3];
figure();
cc = jet(length(B)); %creates colormap
for k = 1:length(B)
abs_z = diffusion(B(k));
f = (0:10:10^5);
p(k) = plot(log10(f),log10(abs_z), 'color', cc(k,:));
hold on;
end
hold off;
function abs_z = diffusion(B)
t_n = 1E-6;
d_n = 1E+6;
q = 1.6E-19;
m_n = 9.11E-31;
w_c = (q * B)/(m_n);
f = (0:10:10^5);
w = f.*(2*pi);
d_real = 1 + ((t_n^2) * ((w.^2) + (w_c^2))) ;
d_img = w.*((t_n)*((t_n.^2) * (((w_c.^2) - w.^2)-1))) ;
d_divider = (1 + ((t_n.^2) * (((w_c.^2) - w.^2).^2)) + ((w.*2*(t_n)).^2)) ;
z = complex(d_real,d_img);
d_m = d_n./d_divider;
abs_z = abs(z).*d_m;
end
sanjay Negi
sanjay Negi 2021 年 10 月 26 日
Its a great help changing color at each iteration. Thanks

サインインしてコメントする。

その他の回答 (1 件)

Samuel Hammarberg
Samuel Hammarberg 2019 年 5 月 30 日

7 投票

When I loop through folders etc. containing data I want to plot, I usually use the 'DisplayName' option.
Example of what my plotscripts typically includes.
% This script plots force vs displacement stored in the Data struct.
figure, hold on
h = legend('show','location','best');
set(h,'FontSize',12);
for i=1:length(Data) % Data is a struct containing my data.
x = Data(i).displacement;
y = Data(i).force;
name = Data(i).name; % Some text string which i usually take from the folder of each data set in Data.
plot(x,y,'-x','LineWidth',2,'MarkerSize',2,'DisplayName',name)
end

カテゴリ

Community Treasure Hunt

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

Start Hunting!

Translated by