Clear Filters
Clear Filters

Single Plot Command is Creating Numerous Legend Entries

30 views (last 30 days)
Holden
Holden on 25 Jun 2024 at 13:51
Commented: dpb on 25 Jun 2024 at 15:38
Hello All
I have a script that I wrote in R2022 and worked fine.
However, now in R2023b, this plot command is creating tons of legend entries (even though the 'for' loop only runs 5 times). The code is unchanged from the original but now does not work the same. Any ideas how to fix this or why this changed?
Code:
% Plot Data
for i = 1:numRuns
line = plot(times{i}/1000,forces{i},'^','Color',colors(i,:),'MarkerSize',10,'DisplayName',dataNames{i});
hold on;
line2 = plot(times{i}/1000,forces{i},'-','Color',colors(i,:),'LineWidth',0.5,'HandleVisibility','off');
xline(t1(i),'--','Color',colors(i,:),'LineWidth',2,'DisplayName','Prolapse');
xline(t2(i),'-','Color',colors(i,:),'LineWidth',2,'DisplayName','Twist');
end
  3 Comments
Holden
Holden on 25 Jun 2024 at 14:06
I can try that - I was mostly confused because both the code and data are unchanged from when I originally ran it on R2022
dpb
dpb on 25 Jun 2024 at 15:38
I suspect that isn't really the case but that there's something been changed you're just not seeing/remembering.
The behavior of plot with respect to its rules on lines hasn't changed as @Steven Lord note regarding column-oriented behavior. The fact it is in a cell array instead of normal 2D numeric array means the content of each cell that you're plotting can be any number of elements/orientation. If it were by row instead, then each column in that internal cell array would turn into another line -- but we simply have no way to see what is in the cell array and, if it were inadvertently modified since the last time you ran the code it wouldn't be possible to reproduce that prior result.
But, almost certainly, it's the data, not a change in MATLAB plot behavior...

Sign in to comment.

Answers (1)

Steven Lord
Steven Lord on 25 Jun 2024 at 14:32
From the documentation for the plot function, in the description of the X input argument: "Alternatively, specify X and Y as matrices of equal size. In this case, MATLAB plots each column of Y against the corresponding column of X. For example:"
So using the example given in that description, we should get three lines plotted (and three lines in the legend) since the X and Y inputs each have three columns.
h = plot([1 2 3; 4 5 6],[7 8 9; 10 11 12])
h =
3x1 Line array: Line Line Line
legend show

Community Treasure Hunt

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

Start Hunting!