MATLAB: How to create a legend that explaines scatter plot by marker size

legend

I am creating a bubble chart (scatter plot with different marker sizes). This works fine but I would need a legend to show the range of the parameter that defines the size of the marker.
The code for the plot looks like this:
scatter(r1,r2,a);
a is an array, that determines the size of the markers.
So I am looking for a legend like the one I attached.
Appreciate any help, thanks a lot!

Best Answer

since you're using scatter one way around this is by doing this:
r1 = randi(10,1,10);
r2 = randi(10,1,10);
a = 30*randi(5,1,10);
bubsizes = unique(a)';
legentry=cell(size(bubsizes));
figure,hold on
for ind = 1:numel(bubsizes)
bubleg(ind) = plot(0,0,'ro','markersize',sqrt(bubsizes(ind)),'MarkerFaceColor','red');
set(bubleg(ind),'visible','off')
legentry{ind} = num2str(bubsizes(ind));
end
h = scatter(r1,r2,a,'r','MarkerFaceColor','red')
legend(legentry)
I couldn't find a quick solution to the scatter plot marker area to plot's "markersize" but the sqrt() of the scatterplot marker area visually looks to be about the same.