Creating a Tabular Legend
62 views (last 30 days)
Show older comments
I would like to create a legend with an additional column for a number to be inserted next to the variable name as follows:

However, I'm unsure as to how I can achieve this layout - I have tried searching for a function like gridlegend, but I cannot get it to work in this format. Any ideas as to how I can get this implemented? Thanks in advance.
0 Comments
Accepted Answer
Sindar
on 22 Apr 2020
Not a good solution, but with some fiddling gets pretty close:
myleg=compose('%-8s%10d',["Boot";"Engine";"Wheel"],[1 3 5]');
myl=legend(myleg)
title(myl,sprintf('%9s %-12s%s','Line','Location','Number'))
More Answers (1)
Ameer Hamza
on 22 Apr 2020
Try this
ax = axes();
hold(ax);
plot(rand(1,10), 'LineWidth', 2);
plot(rand(1,10), 'LineWidth', 2);
plot(rand(1,10), 'LineWidth', 2);
labels1 = {'boot', 'engine', 'wheel'};
labels2 = {'1', '3', '5'};
labels = cellfun(@(x,y) {sprintf('%10s%9s', x, y)}, labels1, labels2);
l = legend(labels, ...
'FontName', 'FixedWidth');
titles = {'Line', 'Location', 'Number'};
titles = sprintf('%4s%14s%10s', titles{:});
l.Title.String = titles;

0 Comments
See Also
Categories
Find more on Legend in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!