How do I adjust legend fontsize after changing legend marker size?

92 views (last 30 days)
There seems to be some kind of glitch in my Matlab program (Using R2016b). I am following the steps to change the marker size from this this URL. I can use their example and it will indeed change the marker size:
plot(1:10, 'o-');
hold on
plot(5:12, 'r*--'); %// example plots
[~, objh] = legend({'one plot', 'another plot'}, 'location', 'NorthWest', 'Fontsize', 14);
%// set font size as desired
objhl = findobj(objh, 'type', 'line'); %// objects of legend of type line
set(objhl, 'Markersize', 12); %// set marker size as desired
However, unlike their example, the fontsize in my image stays fixed. If I try to change fontsize through normal methods, for example,
[~, objh] = legend({'one plot', 'another plot'}, 'Fontsize', 25);)
it will change the box size of the legend, but not the font, as seen in the image below. The font size only becomes fixed when I designate a second output variable in the legend command, in this case, by assigning objh1. So if I just wrote
leg = legend({'one plot', 'another plot'}, 'Fontsize', 25);)
then it would change the font as expected. Because the example in my original URL changes font size correctly, I think this is a problem with my MATLAB software. Is it a version problem or something unusual on my end? I did reinstall the software and still have the same problem.
  4 Comments
Wonmo Sung
Wonmo Sung on 2 Apr 2018
I tried above in MATLAB 2018a. It seems like this issue is already fixed.
pcidreir
pcidreir on 1 May 2019
I'm actually experiencing a similar issue on a subplot. When I increase the lengend size, the box increases, but the text font remains the same. I'm using matlab 2018a

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 1 May 2019
Edited: Adam Danz on 7 Oct 2024
The only currently supported output to legend() is the legend handle (link to docmentation). Starting in R2024b, calling legend with multiple outputs will throw a warning.
When the undocumented, second output is included, it interferes with the ability to change the fontsize of the legend (using r2019a).
Successful example
plot(magic(3))
lh = legend('First', 'Second', 'Third');
lh.FontSize = 14; %or set(lh, 'FontSize', 14)
Buggy example
plot(magic(3))
[lh, lineObj] = legend('First', 'Second', 'Third');
lh.FontSize = 14; %or set(lh, 'FontSize', 14)
If the fontsize is specified in the call to legend, the function behaves as expected.
[lh, lineObj] = legend('First', 'Second', 'Third', 'FontSize', 14); % OK
Other differences when the second output is included are
  • AutoUpdate property is set to 'off' rather than 'on'
  • the UIContextMenu is an empty graphics place holder rather than the default legend context menu.
  7 Comments
Afiq Azaibi
Afiq Azaibi on 7 Oct 2024
Starting in R2024b, calling legend with multiple outputs will throw a warning. It will continue to function as it has previously.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!