MATLAB: Controlling the size of legend markers separately from the font size of legend labels

legendMATLABplotundocumented

I need to control the markers in my legend separately from the font size of the legends' labels. Inspired by this previous answer, I used this code, in Matlab 2016a:
x = 1:10;
plot(x, 1*x, 'o')
hold on
plot(x, 2*x, 's')
h_legend = legend({'one','two'});
objhl = findobj(h_legend, 'type', 'line'); % objects of legend of type patch
set(objhl, 'Markersize', 99); % set marker size as desired
Funnily enough, this solution worked for me for a few plots. However, after a while, now whatever I type in place of the 99, makes no difference. If I change the 'line' into 'patch, that also makes no difference. The problem I guess comes from the fact that objhl is actually empty:
>> objhl = findobj(h_legend, 'type', 'patch')
objhl =
0x0 empty GraphicsPlaceholder array.
Any thoughts? Many thanks!

Best Answer

A new MATLAB graphics system is introduced from MATLAB R2014b onwards. Your code will work for the older system I guess.
Use the code below if you are using MATLAB R2014b or later.
clc; close all;
x = 1:10;
plot(x, 1*x, 'o');
hold on;
plot(x, 2*x, 's');
[~, objh] = legend({'one','two'}); % Instead of "h_legend" use "[~, objh]"
objhl = findobj(objh, 'type', 'line');
set(objhl, 'Markersize', 15);
legendSize.png
Related Question