MATLAB: Fill legend rectangles with color

legendplotplotting

hello,
I'd like to fill the legend rectangles in color (not just the frame as in the picture), how it can be done?
thanks!

Best Answer

Try this example
surf(peaks,'facecolor','none') % plot peaks
surface(membrane,'facecolor','none') % plot membrane
h = legend('surf','memb'); % show legend
h1 = get(h,'children'); % get handle of each object in legend
for i = 1:length(h1) % loop through each object
if strcmp(get(h1(i),'type'),'patch')% if an object is a patch
cc = get(h1(i),'edgecolor'); % get edgecolor
set(h1(i),'facecolor',cc) % set facecolor
end
end