An alternative to text, legend, or annotation commands, this function allows you to label objects directly on a plot by entering the handle of a plotted object and a string. Label text is automatically colored to match plotted data, or text color can be entered manually.
h = label(...)
label(h,'string') places 'string' near the leftmost data described by handle h.
label(...,'location',LocationString) specifies location of the string. LocationString can be any of the following:
'left' or 'west' (default)
'right' or 'east'
'top' or 'north'
'bottom' or 'south'
'center' or 'middle'
label(...,'TextProperty',PropertyValue) specifies text properties as name-value pairs.
label(...,'slope') attempts to angle text following the local slope of the data.
htext = label(...) returns the handle htext of the newly-created text object.
I very much appreciate the customizability of the labels. Users should be aware that they should use linspace functions (and not just endpoints) when plotting straight lines with labels. When I plotted using endpoints and set the label location to 'center', the label was displayed at one end of the line because this function locates the centermost point and positions the label there. Regardless of the heads-up, it's a handy and easy-to-use function.
Great for labelling plots. Will it also work for 3d plots from plot3 command?
Actually I have done it myself. Like i said, very easy. But this site doesn't seem to allow me uploading mods. So maybe you could update by yourself?
Hello! Thanks for the code. A little suggestion: maybe extend the functionality to "plot3" (3d line plot), I estimated that only to write short pieces of codes would be sufficient.
That's a great suggestion, Simon. I have included it in today's fixed version.
The pbaspect function returns all ones in my tests on 2012b, so I used Kelly Kearney's plotboxpos function instead.
I'm not sure about the precise lines needed for a cross-version compatible input check. Although some sort of check would be nice in terms of standard of coding, it is not required as the behavior of the object is actually the same. Same properties, same syntax. Moreover the error message triggered within the code if you do not input a object handle is pretty clear.
However, I did edit the function regarding the computation of the slope. Although you account for gradient and axes scaling, we should also consider that the axes are not the same size. My boxplots are rarely square, that's why the slope was always off in my use of the function. The default matlab boxplot is almost square on most platform so it was not too bad by default, but when you play with the window size...
I suggest you edit the <if followSlope> as:
gpba = pbaspect(gca); %Size of the boxplot
gcaf = gpba(2)/gpba(1);
% slope is scaled because of axes size:
apparentTheta = atand(gcaf*gradient(ydata,xdata).*(gcax(2)-gcax(1))/(gcay(2)-gcay(1)));
In other words, you multiply the real slope by the virtual axes scaling AND by the physical axes scaling.
Now it also means that one must not touch the boxplot size (or window size) after that, so that the label should be last called. I guess a futur evolution of the function should be to make the label position dynamic -- but that's a little too advanced for me...
Anyway, thanks again for the code!
Thanks for the feedback, Simon. Indeed, just yesterday I found that R2014b breaks this function. I will not have time to provide a fix anytime soon, but feel free to edit this function and upload a fixed version. Editing will likely require a few "if verLessThan" statements.
This function looks great! It's actually weird that matlab doesn't provide this by default.
But... your function doesn't work anymore! Since 2014b, handles are objects... which means label displays an error 'Input must be numeric' when using it. I've just commented the error line and the function goes through, however it seems that some functionnalities are now not working properly.
In particular, the slope function doesn't follow correctly the slope of a line. And the location seems to be wrong too -- but maybe i'm not using the function properly?
Any chance you will work on patching it in a near future? It would be a great tool...
Updated for R2014b and fixed aspect ratio as per Simon Tardivel's suggestion. Thanks Simon and another FEX user Matthias for pointing out the R2014b bug.
A bug fix related to finding label pinning points. New examples added.
Improved input/error checking.
Inspired: SI Psychrometric Chart