One of my blog readers, Henrik Toft, reported an undocumented behavior of the well-known scatter plot function: scatter returns a handle to an hggroup-type object that contains child handles of the data points (patch-type objects). This enables easy manipulation of specific points (for example, changing the color or marker of interesting data points).
The undocumented behavior is that when the scatter plot contains more than 100 points, the returned hggroup object only has a single child – a handle to a unified patch object that contains all the data points. This prevents customization of specific points: color can be customized via the cdata property, but this is not trivial; the marker cannot be customized at all.
Both Henrik and I initially thought this was a simple bug. But when Henrik reported this issue to MathWorks support, he got the response that this is indeed the expected behavior (i.e., not a bug but a feature), which is not documented. MathWorks did not say whether this was because they didn’t think users need more than 100 child handles, or perhaps an issue with memory/performance. Whatever the reason, I feel that this behavior/feature should at least be documented:
>> hggroup = scatter(rand(99,1),rand(99,1)); >> size(get(hggroup,'Children')) ans = 99 1 >> hggroup = scatter(rand(100,1),rand(100,1)); >> size(get(hggroup,'Children')) ans = 100 1 >> hggroup = scatter(rand(101,1),rand(101,1)); >> size(get(hggroup,'Children')) ans = 1 1
Workaround: use the ‘v6’ option. Unfortunately, this option is deprecated and marked as obsolete by Matlab, with the warning that it will be removed in a future Matlab version. I hope this will not be done until a better workaround for the above-reported issue is implemented in the scatter function:
>> hPatches = scatter('v6',rand(101,1),rand(101,1)); Warning: The 'v6' argument to SCATTER is deprecated, and will no longer be supported in a future release. > In usev6plotapi>warnv6args at 84 In usev6plotapi at 40 In scatter at 39 >> size(hPatches) ans = 101 1
If you have detected any other undocumented behavior, function or feature in Matlab, please report them in the comments section below, or directly by email to me: altmany at gmail dot com.
Interesting. I use scatter with thousands of points all the time. Perhaps I am misusing it then? I don’t really need the ability to access each data marker but scatter is just an easy function to work with.