Adding Legends to multiple lines in an x-y plot of data imported from Excel sheets in MATLAB ?
Show older comments
Hi, I have the following code:
[~,sheets] = xlsfinfo('C:\Users\Bilal Ahmad\Desktop\Complete Simulations\Microphone Array 1_AllSources\AllSource_MicArr1.xlsx');
for i = 1:length(sheets)
data{i} = xlsread('C:\Users\Bilal Ahmad\Desktop\Complete Simulations\Microphone Array 1_AllSources\AllSource_MicArr1.xlsx',sheets{i},'A4:T6');
d = data{i}(:,7);
tau_g = data{i}(:,9);
plot(d,tau_g,'-x');
hold all
grid on
xlabel('distance d b/w mic pairs[cm]');
ylabel('\tau_g_r_o_u_n_d');
title('Relationship between \tau ground and distance between Mic Pairs for Array 1');
end
when i run this code. i get a following plot

Now, As it is clear in this plot that i am getting data from 16 different sheets that belongs to 16 different sources. My problem is I want to show labels to each 16 different lines so that i come to know which line belong to which sheet of data? Can anybody help me in this regard. Thank you
Accepted Answer
More Answers (2)
Ahmad Bilal
on 8 Oct 2018
3 Comments
Ahmad Bilal
on 8 Oct 2018
madhan ravi
on 8 Oct 2018
Ofcourse gimme sometime , will get back after a work.
Ahmad Bilal
on 8 Oct 2018
madhan ravi
on 8 Oct 2018
Edited: madhan ravi
on 8 Oct 2018
[~,sheets] = xlsfinfo('C:\Users\Bilal Ahmad\Desktop\Complete Simulations\Microphone Array 3_AllSources\AllSrc_MicArray3.xlsx');
lineColor = {'k', 'r', 'b', 'm','c','g','k','y','r','g','m','c','y','b','k','g'};
linestyle = {'-'; '--'; ':'; '-.';'-x';':';'-^';'->';'-v';'-s';'-p';'--';'-x';'-s';'-p';'--x'};
Legend = cell(16,1);
for i = 1:length(sheets)
data{i} = xlsread('C:\Users\Bilal Ahmad\Desktop\Complete Simulations\Microphone Array 3_AllSources\AllSrc_MicArray3.xlsx',sheets{i},'A4:T8');
d = data{i}(:,7);
tau_g = data{i}(:,9);
hold all
grid on
Legend{iter} = strcat('source', num2str(iter));
options = {'color',lineColor{j}, 'LineStyle', linestyle{j}};
plot(d, tau_g, options{:})
xlabel('distance d b/w mic pairs[cm]');
ylabel('\tau_g_r_o_u_n_d');
title('Relationship between \tau ground and distance between Mic Pairs for Array 3');
end
legend(Legend)
11 Comments
Ahmad Bilal
on 9 Oct 2018
Ahmad Bilal
on 9 Oct 2018
madhan ravi
on 9 Oct 2018
Edited: madhan ravi
on 9 Oct 2018
Then use the markers as it shows you(choose from the 5 options shown to you) that is Error using plot '-x' is not a valid value. Use one of these values: '-' | '--' | ':' | '-.' | 'none'.
Ahmad Bilal
on 9 Oct 2018
Ahmad Bilal
on 9 Oct 2018
madhan ravi
on 9 Oct 2018
Edited: madhan ravi
on 9 Oct 2018
Yup, don’t use none because you won’t see any line .
Ahmad Bilal
on 9 Oct 2018
Ahmad Bilal
on 9 Oct 2018
madhan ravi
on 9 Oct 2018
numel(Linestyle) what does it give?
Ahmad Bilal
on 9 Oct 2018
madhan ravi
on 9 Oct 2018
Edited: madhan ravi
on 11 Oct 2018
Then accept this answer to indicate the proper one
Categories
Find more on Legend 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!