Consider the following example:
x = magic(3);
figure(1); clf(1);
plot( x, '-r', 'DisplayName', 'Magic' );
legend( 'show' );
The resulting legend entries in MATLAB R2014a are
getcolumn(Magic,1)
getcolumn(Magic,2)
getcolumn(Magic,3)
The problem stems from function [leg,labelhandles,outH,outM] = legend(varargin) in legend.m (Copyright 1984-2012 The MathWorks, Inc.), line 628:str{k} = get(ch(k),'DisplayName');
More specifically, the function get
- prepends
getcolumn(and - appends
, <Column Number>).
Is there an easy way to display exactly one legend entry (or multiple, but without the pre- and appended strings) for multiple data rows named after DisplayName, which have the same visual properties?
An alternative would of course be to programatically create multiple (or one) legend entries through plot handles (see below), but I would like to keep things short and simple.
One entry:
x = magic(3);
figure(1); clf(1);
h = plot( x, '-r' );
legend( h(1), 'Magic' );
Multiple entries:
x = magic(3);
figure(1); clf(1);
h = plot( x, '-r' );
strL = cell( 1, numel(h) );
for k = 1:numel(h)
strL{k} = sprintf( 'Magic %d', k );
end
legend( h, strL );
In MATLAB R2014b, the problem with getcolumn(Name,Row) does not appear anymore for the first code example.