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

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 Comments

Look at the version history of the legend command at the bottom of this page:
Although I cannot directly see what causes the legend command fail in R2020b in your case, it shows that there were several changes between 2020 and 2023.
I don't see anything fatal, either.
FYI the legend shows for both R2019b and R2021b; but I don't have R2020b installed.
Have you installed the latest patches on your installation?
What if you set breakpoint and then interactively work with legend() to put on individually; maybe a symptom will show up to explain the problem.
ADDENDUM
Since all handles are being included, don't need the handles array; have you tried simply
legend
w/o any argument?
Just one refinement to preallocate the handles array although with only a small number as here the dynamic reallocation won't be a noticeable overhead...
...
iterationNumber = 2;
...
h=gobjects(2+2*iterationNumber,1); % preallocate for graphics object handles
h(1) = scatter(x, y, 'DisplayName', 'XY Points');
h(2) = fill(z, w, 'r', 'DisplayName', 'Polygon');
j=2; % counter for handles
for idx = 1:iterationNumber
j=j+1;
h(j) = scatter(A(idx,:), B(idx,:), 'DisplayName', sprintf('Mode %d', idx));
j=j+1;
h(j) = scatter(C(idx,:), D(idx,:), 'DisplayName', 'Potential');
end
legend(h);
...
Datapoint:
The code shows the legend for me on MacOS 15.6.1 for Intel
As an addtiional datapoint, when I run the code in R2020b, the legend appears. I am using Windows 11.
@Florian - just out of curiosity, what does
hLg=findobj(gcf,'type','legend');
hLg.Interpreter
return

Sign in to comment.

 Accepted Answer

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.

More Answers (1)

Florian
Florian on 15 Oct 2025
Moved: Cris LaPierre on 31 Oct 2025
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.

Products

Release

R2020b

Tags

Asked:

on 14 Oct 2025

Moved:

on 31 Oct 2025

Community Treasure Hunt

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

Start Hunting!