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

Commit f41c6bc

Browse files
committed
Restore append functionality for MATLAB >= R2017a
The addition of 'AutoUpdate' to legend in R2017a breaks the functionality of append. With 'AutoUpdate' turned off we can restore the functionality of legtools, but turning it back on causes our appended legend entries to be deleted. Clearing out the undocumented 'PlotChildrenExcluded' legend property seems to prevent this from occuring This is untested in versions < R2017a See #15
1 parent e693822 commit f41c6bc

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

Readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Append string(s), `newStrings`, to the specified `Legend` object, `legendhandle`
1818

1919
The legend will only be updated with the new strings if the number of strings in the existing legend plus the number of strings in `newStrings` is the same as the number of plots on the associated `Axes` object (e.g. if you have 2 lineseries and 2 legend entries already no changes will be made).
2020

21+
**NOTE**: For MATLAB R2017a or newer, the `legend` object's [`'AutoUpdate'`](https://www.mathworks.com/help/matlab/ref/matlab.graphics.illustration.legend-properties.html#bt7bgi4-1-AutoUpdate) property *must* be set to `'off'` before adding additional lineseries to the plot.
22+
2123
#### Examples
2224
##### Adding one legend entry
2325
% Sample data
@@ -154,4 +156,4 @@ For a single dummy legend entry, `plotParams` is defined as a cell array of stri
154156
plotParams = {{'Color', 'Red'}, {'Color', 'green'}};
155157
legtools.adddummy(lh, newStrings, plotParams)
156158

157-
![addummy2](https://github.com/sco1/sco1.github.io/blob/master/legtools/adddummy2.png)
159+
![addummy2](https://github.com/sco1/sco1.github.io/blob/master/legtools/adddummy2.png)

legtools.m

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
% remove - Remove one or more legend entries
1212
% adddummy - Add one or more entries to the legend for unsupported graphics objects
1313
%
14+
% NOTE:
15+
% For MATLAB versions >= R2017a, the legend object's 'AutoUpdate'
16+
% property must be set to 'off' before using this utility
17+
%
1418
% See also legend
1519

1620
methods (Static)
@@ -38,6 +42,8 @@ function append(lh, newStrings)
3842

3943
newStrings = legtools.strcheck('append', newStrings);
4044

45+
legtools.autoupdatecheck(lh)
46+
4147
% To make sure we target the right axes, pull the legend's
4248
% PlotChildren and get their parent axes object
4349
parentaxes = lh.PlotChildren(1).Parent;
@@ -66,6 +72,19 @@ function append(lh, newStrings)
6672
'Ignoring extra legend entries');
6773
end
6874
lh.String = newlegendstr;
75+
76+
if ~verLessThan('matlab', '9.2')
77+
% The addition of 'AutoUpdate' to legend in R2017a breaks
78+
% the functionality of append. With 'AutoUpdate' turned off
79+
% we can restore the functionality of legtools, but turning
80+
% it back on causes our appended legend entries to be
81+
% deleted. Clearing out the undocumented
82+
% 'PlotChildrenExcluded' legend property seems to prevent
83+
% this from occuring
84+
%
85+
% NOTE: This is untested in versions < R2017a
86+
lh.PlotChildrenExcluded = [];
87+
end
6988
end
7089

7190

@@ -312,5 +331,17 @@ function verchk()
312331
end
313332
end
314333
end
334+
335+
function autoupdatecheck(lh)
336+
% If we're using R2017a or newer, we need to make sure that
337+
% 'AutoUpdate' is off
338+
if ~verLessThan('matlab', '9.2')
339+
if ~strcmp(lh.AutoUpdate, 'off')
340+
lh.AutoUpdate = 'off';
341+
warning('legtools:autoupdatecheck:AutoUpdateNotOff', ...
342+
'Input legend object''s ''AutoUpdate'' property has been set to ''off''')
343+
end
344+
end
345+
end
315346
end
316347
end

0 commit comments

Comments
 (0)