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

Commit d2e9d10

Browse files
committed
Release v2.3.0
2 parents b655c27 + 043d85e commit d2e9d10

2 files changed

Lines changed: 80 additions & 22 deletions

File tree

Readme.md

Lines changed: 7 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
@@ -62,6 +64,8 @@ The legend will only be updated with the new strings if the number of strings in
6264
#### Description
6365
Rearrange the entries of the specified `Legend` object, `legendhandle`, so they are in the order specified by the vector `newOrder`. `newOrder` must be the same length as the number of legend entries in `legendhandle`. All elements of order must be unique, real, positive, integer values.
6466

67+
**NOTE**: `legtools.append` is currently unsupported for MATLAB >= R2017a
68+
6569
#### Example
6670
% Sample data
6771
x = 1:10;
@@ -87,6 +91,8 @@ If `removeidx` specifies all the legend entries the `Legend` object, `legendhand
8791

8892
If a legend entry to be removed is one generated by `legtools.adddummy`, its corresponding Chart Line Object will also be deleted.
8993

94+
**NOTE**: `legtools.remove` is currently unsupported for MATLAB >= R2017a
95+
9096
#### Example
9197
% Sample data
9298
x = 1:10;
@@ -154,4 +160,4 @@ For a single dummy legend entry, `plotParams` is defined as a cell array of stri
154160
plotParams = {{'Color', 'Red'}, {'Color', 'green'}};
155161
legtools.adddummy(lh, newStrings, plotParams)
156162

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

legtools.m

Lines changed: 73 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,22 @@
1-
classdef legtools
1+
classdef (Abstract) legtools
22
% LEGTOOLS is a MATLAB class definition providing the user with a set of
33
% methods to modify existing Legend objects.
44
%
55
% This is an HG2 specific implementation and requires MATLAB R2014b or
66
% newer.
77
%
88
% legtools methods:
9-
% append - Add one or more entries to the end of the legend
10-
% permute - Rearrange the legend entries
11-
% remove - Remove one or more legend entries
12-
% adddummy - Add one or more entries to the legend for unsupported graphics objects
9+
% append - Add one or more entries to the end of the legend
10+
% permute - Rearrange the legend entries
11+
% remove - Remove one or more legend entries
12+
% adddummy - Add one or more entries to the legend for unsupported graphics objects
13+
%
14+
% NOTE:
15+
% For MATLAB versions >= R2017a, the legend object's 'AutoUpdate'
16+
% property must be set to 'off' before using this utility
1317
%
1418
% See also legend
1519

16-
methods
17-
function obj = legtools
18-
% Dummy constructor so we don't return an empty class instance
19-
clear obj
20-
end
21-
end
22-
2320
methods (Static)
2421
function append(lh, newStrings)
2522
% APPEND appends strings, newStrings, to the specified Legend
@@ -45,6 +42,8 @@ function append(lh, newStrings)
4542

4643
newStrings = legtools.strcheck('append', newStrings);
4744

45+
legtools.autoupdatecheck(lh)
46+
4847
% To make sure we target the right axes, pull the legend's
4948
% PlotChildren and get their parent axes object
5049
parentaxes = lh.PlotChildren(1).Parent;
@@ -73,6 +72,19 @@ function append(lh, newStrings)
7372
'Ignoring extra legend entries');
7473
end
7574
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
7688
end
7789

7890

@@ -84,7 +96,18 @@ function permute(lh, order)
8496
% real, positive, integer values.
8597
legtools.verchk()
8698

87-
% TODO: Add check for presence of order
99+
% Temporarily throw an error for MATLAB >= R2017a
100+
if ~verLessThan('matlab', '9.2')
101+
error('legtools:permute:NotImplementedError', ...
102+
'legtools.permute is currently not functional in MATLAB >= R2017a', ...
103+
)
104+
end
105+
106+
if ~exist('order', 'var') || isempty(order)
107+
error('legtools:permute:EmptyOrderInput', ...
108+
'No permute order provided' ...
109+
);
110+
end
88111

89112
lh = legtools.handlecheck('permute', lh);
90113

@@ -118,6 +141,13 @@ function remove(lh, remidx)
118141
% object is deleted
119142
legtools.verchk()
120143
lh = legtools.handlecheck('remove', lh);
144+
145+
% Temporarily throw an error for MATLAB >= R2017a
146+
if ~verLessThan('matlab', '9.2')
147+
error('legtools:remove:NotImplementedError', ...
148+
'legtools.remove is currently not functional in MATLAB >= R2017a', ...
149+
)
150+
end
121151

122152
% Catch length issues, let MATLAB deal with the rest
123153
if numel(unique(remidx)) > numel(lh.String)
@@ -139,8 +169,6 @@ function remove(lh, remidx)
139169
);
140170
end
141171

142-
143-
144172
if numel(unique(remidx)) == numel(lh.String)
145173
delete(lh);
146174
warning('legtools:remove:LegendDeleted', ...
@@ -198,6 +226,8 @@ function adddummy(lh, newStrings, plotParams)
198226

199227
newStrings = legtools.strcheck('adddummy', newStrings);
200228

229+
legtools.autoupdatecheck(lh)
230+
201231
% See if we have a character input for the single addition case
202232
% and put it into a cell. Double nest the cells so behavior is
203233
% consistent with a cell array of cells for multiple new dummy
@@ -215,8 +245,6 @@ function adddummy(lh, newStrings, plotParams)
215245
end
216246
end
217247

218-
% TODO: More plotParams error checking
219-
220248
parentaxes = lh.PlotChildren(1).Parent;
221249

222250
washeld = ishold(parentaxes); % Set a flag for previous hold state ofthe parent axes
@@ -229,16 +257,28 @@ function adddummy(lh, newStrings, plotParams)
229257
% If parentaxes wasn't previously held, turn hold back off
230258
hold(parentaxes, 'off');
231259
end
232-
233260
legtools.append(lh, newStrings); % Add legend entries
261+
262+
if ~verLessThan('matlab', '9.2')
263+
% The addition of 'AutoUpdate' to legend in R2017a breaks
264+
% the functionality of append. With 'AutoUpdate' turned off
265+
% we can restore the functionality of legtools, but turning
266+
% it back on causes our appended legend entries to be
267+
% deleted. Clearing out the undocumented
268+
% 'PlotChildrenExcluded' legend property seems to prevent
269+
% this from occuring
270+
%
271+
% NOTE: This is untested in versions < R2017a
272+
lh.PlotChildrenExcluded = [];
273+
end
234274
end
235275

236276
end
237277

238278
methods (Static, Access = private)
239279
function verchk()
240280
% Throw error if we're not using R2014b or newer
241-
if verLessThan('matlab','8.4')
281+
if verLessThan('matlab', '8.4')
242282
error('legtools:UnsupportedMATLABver', ...
243283
'MATLAB releases prior to R2014b are not supported' ...
244284
);
@@ -289,13 +329,13 @@ function verchk()
289329
% out our error
290330
error(msgID, ...
291331
'Invalid Data Type Passed: %s\n\nData must be of type: ''%s'', ''%s'', or ''%s''', ...
292-
class(newString), class(cell(1)), class(char('')), class(string('')) ...
332+
class(newString), class(cell(1)), class(''), class("") ...
293333
);
294334
else
295335
% Error message for MATLAB versions older than R2016b
296336
error(msgID, ...
297337
'Invalid Data Type Passed: %s\n\nData must be of type: ''%s'' or ''%s''', ...
298-
class(newString), class(cell(1)), class(char('')) ...
338+
class(newString), class(cell(1)), class('') ...
299339
);
300340
end
301341
end
@@ -319,5 +359,17 @@ function verchk()
319359
end
320360
end
321361
end
362+
363+
function autoupdatecheck(lh)
364+
% If we're using R2017a or newer, we need to make sure that
365+
% 'AutoUpdate' is off
366+
if ~verLessThan('matlab', '9.2')
367+
if ~strcmp(lh.AutoUpdate, 'off')
368+
lh.AutoUpdate = 'off';
369+
warning('legtools:autoupdatecheck:AutoUpdateNotOff', ...
370+
'Input legend object''s ''AutoUpdate'' property has been set to ''off''')
371+
end
372+
end
373+
end
322374
end
323375
end

0 commit comments

Comments
 (0)