Adding Legends to multiple lines in an x-y plot of data imported from Excel sheets in MATLAB ?

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

 Respuesta aceptada

Legend=cell(16,1)
for iter=1:16
Legend{iter}=strcat('sheet', num2str(iter));
end
legend(Legend)

5 comentarios

If it’s working accept the answer if not let know what’s additionally required.
It works. Now the image is shown as follows :
Still,I am having difficulty in differentiating different colors with respect to each sheets. Can you suggest any further enhancement in code so that one can easily differentiate between values.
Thank you
My question is how can i fit this code in the above mentioned code because when I tried it shows an error : index exceeds matrix dimensions
Which line shows error , your question is vague.

Iniciar sesión para comentar.

Más respuestas (2)

Hi this is my now updated code.
[~,sheets] = xlsfinfo('C:\Users\Bilal Ahmad\Desktop\Complete Simulations\Microphone Array 3_AllSources\AllSrc_MicArray3.xlsx');
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 = cell(16,1);
for iter = 1:16
Legend{iter} = strcat('source', num2str(iter));
end
legend(Legend)
lineColor = {'k', 'r', 'b', 'm'};
linestyle = {'-'; '--'; ':'; '-.'};
for j = 1:4
options = {'color',lineColor{j}, 'LineStyle', linestyle{j}};
plot(d(:,j), tau_g(:,j), options{:})
end
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

3 comentarios

Can you help me where I am doing mistake ??
Ofcourse gimme sometime , will get back after a work.
Thank you so much. I will wait for your answer

Iniciar sesión para comentar.

[~,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 comentarios

Thank you so much. However when i try to run this code it still gives me an error as follows:
Error using plot '-x' is not a valid value. Use one of these values: '-' | '--' | ':' | '-.' | 'none'.
and : plot(d, tau_g, options{:}).
Can you help me out why is this the case ??
This is my updated code:
[~,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{i} = strcat('source', num2str(i));
options = {'color',lineColor{i}, 'LineStyle', linestyle{i}};
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)
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'.
You meant by this ??
lineColor = {'k', 'r', 'b', 'm','c','g','k','y','r','g','m','c','y','b','k','g'};
linestyle = {'-'; '--'; ':'; '-.','none','-'; '--'; ':'; '-.','none','-'; '--'; ':'; '-.','none';':'};
Yup, don’t use none because you won’t see any line .
lineColor = {'k', 'r', 'b', 'm','c','g','k','y','r','g','m','c','y','b','k','g'};
linestyle = {'-'; '--'; ':'; '-.','-','-'; '--'; ':'; '-.','-','-'; '--'; ':'; '-.','-';':'};
It is still giving an error : Dimensions of matrices being concatenated are not consistent. Although I have chosen same dimensions.Why it is giving an error ? Can u help me by suggesting me change in code ?
Now It works perfectly fine. Thank you so much for your time and help.
numel(Linestyle) what does it give?
It gives 16. and now it works.Thank you
Then accept this answer to indicate the proper one

Iniciar sesión para comentar.

Preguntada:

el 8 de Oct. de 2018

Editada:

el 11 de Oct. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by