Legend not showing in MATLAB 2020b but works in 2023b

조회 수: 67 (최근 30일)
Florian
Florian 2025년 10월 14일 10:22
이동: Cris LaPierre 2025년 10월 31일 20:33
The following MATLAB script plots multiple scatter points and a filled polygon, adding them dynamically in a loop. I use the DisplayName property for legend entries and then create a legend using an array of handles.
The legend displays correctly in MATLAB 2023b, but in MATLAB 2020b, the legend does not appear.
% Test data
x=rand(1,10);
y=rand(1,10);
iterationNumber = 2;
A=rand(iterationNumber,10);
B=rand(iterationNumber,10);
C=rand(iterationNumber,10);
D=rand(iterationNumber,10);
% Proper polygon for fill
z = [0 4 5 2 1];
w = [0 0 2 4 3];
fig = figure;
hold on;
grid on;
h1 = scatter(x, y, 'DisplayName', 'XY Points');
h2 = fill(z, w, 'r', 'DisplayName', 'Polygon');
handles = [h1 h2];
for idx = 1:iterationNumber
h3 = scatter(A(idx,:), B(idx,:), 'DisplayName', sprintf('Mode %d', idx));
h4 = scatter(C(idx,:), D(idx,:), 'DisplayName', 'Potential');
handles = [handles h3 h4];
end
xlabel('xLabel');
ylabel('yLabel');
legend(handles);
hold off;
Is there a difference in how DisplayName and legend(handles) are handled between MATLAB 2020b and newer versions? What is the recommended way to ensure that legends show correctly in older versions?
  댓글 수: 6
Cris LaPierre
Cris LaPierre 2025년 10월 14일 20:10
As an addtiional datapoint, when I run the code in R2020b, the legend appears. I am using Windows 11.
dpb
dpb 2025년 10월 14일 21:05
@Florian - just out of curiosity, what does
hLg=findobj(gcf,'type','legend');
hLg.Interpreter
return

댓글을 달려면 로그인하십시오.

채택된 답변

Benjamin Kraus
Benjamin Kraus 2025년 10월 31일 20:24
From your latest comments, it seems you have resolved the issue you were facing, but just to confirm.
You asked: "Is there a difference in how DisplayName and legend(handles) are handled between MATLAB 2020b and newer versions?"
No, there are no differences in how DisplayName or legend behaved between R2020b and R2023b that would have impacted your code. I would expect your code to run the same in R2014b and newer.

추가 답변 (1개)

Florian
Florian 2025년 10월 15일 5:59
이동: Cris LaPierre 2025년 10월 31일 20:33
Indeed, when I run it now in R2020b, the legend appears. The issue was solved when I ran clear, clc. I though that handles would be freshly initialized when running the script, but apparently some old version of handles was still in memory yesterday.

카테고리

Help CenterFile Exchange에서 Legend에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by