Skip to content
This repository was archived by the owner on Apr 5, 2019. It is now read-only.

Commit ae66d0e

Browse files
committed
Fix bug in append method
The append method contains a bug when it appends to a legend that has been permuted previously or if the parent axes children have been sorted differently after the legend has been modified. Append assumes the parent axes children are in the same order as the current legend PlotChildren, which may not be the case. For example, after permuting the legend entries, this order may have changed.
1 parent e3a54de commit ae66d0e

1 file changed

Lines changed: 21 additions & 9 deletions

File tree

legtools.m

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,27 @@ function append(lh, newStrings)
5353

5454
% To make sure we target the right axes, pull the legend's
5555
% PlotChildren and get their parent axes object
56-
parentaxes = lh.PlotChildren(1).Parent;
57-
58-
% Get line object handles
59-
plothandles = flipud(parentaxes.Children); % Flip so order matches
60-
61-
% Update legend with line object handles & new string array
62-
newlegendstr = [lh.String newStrings]; % Need to generate this before adding new plot objects
63-
lh.PlotChildren = plothandles;
64-
lh.String = newlegendstr;
56+
ax = lh.PlotChildren(1).Parent;
57+
58+
% Get graphics object handles
59+
axchildren = flip(ax.Children); % Flip so order matches
60+
legchildren = lh.PlotChildren;
61+
62+
% Sort the children of the future legend object in an order
63+
% depending on current legend PlotChildren property, because
64+
% this may not be in the same order as axchildren, e.g. after
65+
% permuting the legend entries
66+
[~,~,icurrent] = intersect(legchildren,axchildren,'stable');
67+
[~,idiff] = setdiff(axchildren,legchildren,'stable');
68+
ifuture = [icurrent;idiff];
69+
axchildren = axchildren(ifuture);
70+
71+
% Strings desired order for future legend
72+
newstr = [lh.String, newStrings];
73+
74+
% Update legend with graphics object handles & new string array
75+
lh.PlotChildren = axchildren;
76+
lh.String = newstr;
6577
end % of append method
6678

6779
function permute(lh, order)

0 commit comments

Comments
 (0)