i see this post
but i don't understan how resolve it
i don't want visualize 'str2' in legend
str1='A';
str2='';
str3='C';
legend(str1,str2,str3,'Location','northwest')
Warning: Ignoring extra legend entries.
> In legend>process_inputs (line 592)
In legend>make_legend (line 319)
In legend (line 263)
In Predator_grafici_Struct (line 67)

 Akzeptierte Antwort

Star Strider
Star Strider am 22 Jul. 2023

2 Stimmen

Perhaps this —
x = 0:10;
y1 = randn(size(x));
y2 = randn(size(x));
y3 = randn(size(x));
figure
hp1 = plot(x, y1, 'DisplayName','A');
hold on
hp2 = plot(x, y2, 'DisplayName','B');
hp3 = plot(x, y3, 'DisplayName','C');
hold off
grid
legend([hp1 hp3], 'Location','best')
.

5 Kommentare

shamal
shamal am 22 Jul. 2023
hold(Ax_Eq, 'on')
if Set.ShowOne
hp1=plot(Ax_Eq,XDates,TEE1,'A','Color','black');
end
hp2=plot(Ax_Eq,XDates,TEE2,'B','Color',[0 0.4470 0.7410]);
if Set.ShowUnfiltered
hp3=plot(Ax_Eq,XDates,TEE3,'C','Color','red');
end
ytickformat(Ax_Eq,'usd');
axis(Ax_Eq, 'tight');
disableDefaultInteractivity(Ax_Eq)
grid(Ax_Eq,"on");
%legend(Ax_Eq,'Unfiltered','Filtred','Location','northwest');
legend([hp1 hp2 hp3],'Location','northwest');
sett=extractBefore(Set.tslist,'.');
title(Ax_Eq,sett);
hold(Ax_Eq, 'off')
Unrecognized function or variable 'hp1'.
Error in Predator_grafici_AppDesigner_Struct (line 58)
legend([hp1 hp2 hp3],'Location','northwest');
Image Analyst
Image Analyst am 22 Jul. 2023
Bearbeitet: Image Analyst am 22 Jul. 2023
Basically, to have the legend() function display only some of the curves you have already plotted, you have to keep track of the handles to the curves you plotted and then you pass in ONLY the handles to the curves you want legend() to show. But you did this
legend([hp1 hp2 hp3],'Location','northwest');
which would show all 3 curves (if all 3 of those existed). However you never assigned hp1 so you can't tell legend to use it because it does not exist - you never created hp1.
@Luca Re — This error:
Unrecognized function or variable 'hp1'.
appears because the first plot is only executed if ‘Set.ShowOne’ is true, so if that condition is not met, ‘hp1’ will not be created.
One approach to fixing that would be to set ‘hp1’ and ‘hp3’ (since it also will only be executed if ‘Set.ShowUnfiltered’ is true) to empty values first —
x = 0:10;
y1 = randn(size(x));
y2 = randn(size(x));
y3 = randn(size(x));
hp1 = [];
hp3 = [];
Set.ShowOne = 0;
Set.ShowUnfiltered = 1;
figure
if Set.ShowOne
hp1 = plot(x, y1, 'DisplayName','A');
end
hold on
hp2 = plot(x, y2, 'DisplayName','B');
if Set.ShowUnfiltered
hp3 = plot(x, y3, 'DisplayName','C');
end
hold off
grid
legend([hp1 hp2 hp3], 'Location','best')
This does not plot ‘hp1’ because the condition ‘Set.ShowOne’ is not met, and because ‘hp1’ has already been defined as ‘empty’, the error you got is not thrown in this code example. The third plot that creates ‘hp3’ plots and shows in the legend because ‘Set.ShowUnfiltered’ is true, and ‘hp3’ over-writes the previously set ‘empty’ value, so it appears in the legend call.
.
shamal
shamal am 23 Jul. 2023
correct! thank
Star Strider
Star Strider am 23 Jul. 2023
As always, my pleasure!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by