Borrar filtros
Borrar filtros

Warning: Ignoring extra legend entries. How do I fix this?

40 visualizaciones (últimos 30 días)
Karl
Karl el 8 de Dic. de 2022
Editada: Voss el 8 de Dic. de 2022
Hello all, I am using a for loop to form a matrix based on user inputs. It works how I want it when there is more than 1 iterations; however, whenever I only use 1 iteration it gives me an error during the graphing section.
clc
clear
close all
%this is an example similar to my code
n = input('how many iterations ');
update = [[],[],[],[],[]];
for k = 1:1:n
input1 = input('input number ');
input2 = input('input number ');
input3 = input('input number ');
input4 = input('input number ');
update = [update; n, input1, input2, input3, input4]
end
ngraph = update(:,[3,5]);
hold on
bar( ngraph, 'grouped' )
hold off
drawnow
xticks( 1:1:n )
ylabel( 'height' )
xlabel('distance')
[MIN, mL] = min(ngraph(:,2));
[MAX, ML] = max(ngraph(:,2));
ylim([0 (MAX+25)])
text(mL,MIN,'Low', 'HorizontalAlignment','left','VerticalAlignment','top')
text(ML,MAX,'High','HorizontalAlignment', 'left', 'VerticalAlignment', 'top')
legend('professionals' ,'kids')
Because when I type update(:,[3,5]) in the command window, it clearly shows 2 values. So i'm confused why it's saying "Warning: Ignoring extra legend entries."
I appreciate any help, thank you.

Respuesta aceptada

Voss
Voss el 8 de Dic. de 2022
Editada: Voss el 8 de Dic. de 2022
When n == 1, bar() gives you a single series (update has one row):
figure
bar([2 3],'grouped') % notice: no orange bars
as opposed to when n > 1:
figure
bar([2 3; 4 5],'grouped') % two columns -> two series (one blue, one orange)
So when you try to apply two legend entries to a single series you get that warning and only the first entry appears in the legend:
figure
bar([2 3],'grouped')
legend('professionals' ,'kids')
Warning: Ignoring extra legend entries.
To avoid that warning and get the legend correct, specify the x-coordinates in bar() along with the y:
figure
bar(1,[2 3],'grouped') % notice: now there is an orange bar
legend('professionals' ,'kids') % and two legend entries
In your code the x-coordinates can be 1:n, so that for n == 1, you get:
clc
clear
% close all
% %this is an example similar to my code
% n = input('how many iterations ');
%
% update = [[],[],[],[],[]]; % by the way, that is the same as: update = [];
%
% for k = 1:1:n
% input1 = input('input number ');
% input2 = input('input number ');
% input3 = input('input number ');
% input4 = input('input number ');
% update = [update; n, input1, input2, input3, input4]
% end
figure
n = 1;
update = [n*ones(n,1) randi(10,n,4)]; % some random inputs
ngraph = update(:,[3,5]);
hold on
bar(1:n, ngraph, 'grouped')
hold off
drawnow
xticks( 1:1:n )
ylabel( 'height' )
xlabel('distance')
[MIN, mL] = min(ngraph(:,2));
[MAX, ML] = max(ngraph(:,2));
ylim([0 (MAX+25)])
text(mL,MIN,'Low', 'HorizontalAlignment','left','VerticalAlignment','top')
text(ML,MAX,'High','HorizontalAlignment', 'left', 'VerticalAlignment', 'top')
legend('professionals' ,'kids')
And for n > 1:
clc
clear
% close all
% %this is an example similar to my code
% n = input('how many iterations ');
%
% update = [[],[],[],[],[]]; % by the way this is the same as: update = [];
%
% for k = 1:1:n
% input1 = input('input number ');
% input2 = input('input number ');
% input3 = input('input number ');
% input4 = input('input number ');
% update = [update; n, input1, input2, input3, input4]
% end
figure
n = 2;
update = [n*ones(n,1) randi(10,n,4)];
ngraph = update(:,[3,5]);
hold on
bar(1:n, ngraph, 'grouped')
hold off
drawnow
xticks( 1:1:n )
ylabel( 'height' )
xlabel('distance')
[MIN, mL] = min(ngraph(:,2));
[MAX, ML] = max(ngraph(:,2));
ylim([0 (MAX+25)])
text(mL,MIN,'Low', 'HorizontalAlignment','left','VerticalAlignment','top')
text(ML,MAX,'High','HorizontalAlignment', 'left', 'VerticalAlignment', 'top')
legend('professionals' ,'kids')

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by