Single Plot Command is Creating Numerous Legend Entries

3 次查看(过去 30 天)
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 个评论
Holden
Holden 2024-6-25
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 2024-6-25
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...

请先登录,再进行评论。

回答(1 个)

Steven Lord
Steven Lord 2024-6-25
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

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by