A few days ago, a reader of the Matlab Desktop blog asked whether it is possible to store plot brushed data in a separate variable for later processing. Data Brushing, first introduced in R2008a, enables interactive selection and marking of plot data points. The brushed data can then be stored in a variable using the context (right-click) menu, or the figure’s Tools/Brushing menu.
Clik here to view.

Saving brushed data to a variable
The said reader has specifically wanted programmatic access, rather than interactivity. The official answer is that data brushing was designed to be an interactive tool, and so this cannot be done. However, this has never stopped us before. So off I went to launch my favorite inspection tool, the UIInspect utility on the figure above (UIInspect will be described in a near-future article), which can be recreated with the following simple code:
t=0:0.2:25; plot(t,sin(t),'.-'); % Now brush some data points... uiinspect(gca);
Clik here to view.

UIInspect-ion of a data-brushed plot (click for details)
A couple of alternative answers to the reader’s question were immediately apparent:
Directly accessing brushed data
First, we notice that data brushing added data-brushing context menus, both of which are called BrushSeriesContextMenu (the duplication is an internal Matlab bug, that does not affect usability as far as I can tell).
Also, an invisible scribe overlay axes has been added to hold the new annotations (data brushing is considered an annotation; scribe axes deserve a separate article, which they will indeed get someday).
More importantly for our needs, we see a new line item called ‘Brushing’, which displays the red lines and data points that we seek. We can now easily get the brushed data using this line’s XData and YData properties: non-brushed data points simply have NaN values:
Clik here to view.

UIInspect-ion of the data-brushing line
hBrushLine = findall(gca,'tag','Brushing'); brushedData = get(hBrushLine, {'Xdata','Ydata'}); brushedIdx = ~isnan(brushedData{1}); brushedXData = brushedData{1}(brushedIdx); brushedYData = brushedData{2}(brushedIdx); % and similarly for ZData in 3D plots
Accessing brushing callbacks
Yet another way of approaching the problem is to use the available callback functions built-into the data-brushing functionality. We can access either the BrushSeriesContextMenu or the figure’s Tools/Brushing menu. I will leave the former (context-menu) approach as an exercise to the reader, and just describe the figure’s menu approach.
As I have already explained in a dedicated article, figure menu-bar actions are accessible via their handles, and we can retrieve that using a unique tag (well, most of the time – read that article for details). In our case, the Tools/Brushing/Create-new-variable menu item has the unique tag ‘figDataManagerNewVar’. So let’s use it:
>> hNewVarMenuItem = findall(gcf,'tag','figDataManagerNewVar') hNewVarMenuItem = 742.000244140625 >> hNewVarCallback = get(hNewVarMenuItem,'callback') hNewVarCallback = @datamanager.newvar >> hNewVarCallback(gcf) % activate the callback % => set 'ans' as the new variable holding the brushed data >> ans ans = 6.4 0.116549204850494 6.6 0.311541363513379 6.8 0.494113351138609 7 0.656986598718789 7.2 0.793667863849153 7.4 0.898708095811627 7.6 0.967919672031486 7.8 0.998543345374605 ... ...
Of course, we could also have gone the hard way, via the scribe axes and the annotations route. For masochistic people like me it could even be a worthwhile challenge. But for all other normal people, why bother when there are such simple alternatives, if we only knew how to find them. Image may be NSFW.
Clik here to view.
Related posts:
- Controlling plot data-tips Data-tips are an extremely useful plotting tool that can easily be controlled programmatically....
- Draggable plot data-tips Matlab's standard plot data-tips can be customized to enable dragging, without being limitted to be adjacent to their data-point. ...
- Performance: accessing handle properties Handle object property access (get/set) performance can be significantly improved using dot-notation. ...
- Accessing the Matlab Editor The Matlab Editor can be accessed programmatically, for a wide variety of possible uses - this article shows how....