MATLAB Answers

How to use the data brush tool to automatically save selected points in multiple line plots

189 views (last 30 days)
I am plotting multiple sets of data in a line plot. 
plot(x1, y1, x2, y2, ...)
I use the brushing tool to select the points of data I would like to further evaluate. I would like to reassign variables with specific names to the new selected sections automatically. Is there a way to do this without having to go to Tools -> Brushing -> Create new variable, and then assign each variable one at a time?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 24 Sep 2021
Edited: MathWorks Support Team on 24 Sep 2021
MATLAB does not currently have the functionality to output multiple variables at once. The enhancement suggestion has been forwarded to the appropriate production team. It will be considered for inclusion in a future release of MATLAB. However, there are several options to work around this issue.
Option 1:
Use right-click -> Create Variable instead of going through the menu bar. Unfortunately this only saves a little time, as the user still has to create each variable individually.
Option 2:
Use a third party toolbox that can handle this.
However, MathWorks does not guarantee or warrant the use or content of these third-party submissions. Any questions, issues, or complaints should be directed to the contributing author.
Option 3:
Use the undocumented 'BrushData' property of a brush-able graphics object to see which data points are selected. Note that because it is undocumented, it may be removed in a future release of MATLAB. The 'BrushData' property is a "uint8" array of 0s and 1s, with 1s indicating data points that are selected. This must be transformed into logical array before it can be used to index into the data arrays.
See the attached "saveBrushDataScript.m" file for an example of the use of this property.
Unfortunately, this requires more interaction from the user, and probably means going back and forth from the figure and the command window to specify when the current selection is done. Variables will also have to be named programmatically, or through interaction in the the command window.
Option 4:
Similar to Option 3, use the 'BrushData' property in conjunction with the Data Brush tool's 'ActionPostCallback'. This will allow an action to be taken every time the Data Brush finishes a selection action (on the release of the mouse button). The callback can extract the 'BrushData' property from each line, and get the selected data by indexing into the 'XData' and 'YData' properties. The output happens in the background with no interaction from the user.
Unfortunately, this action will occur every time a selection is complete. The user may still be selecting more data points or removing them (using the Ctrl and Shift keys). The variables will also need to be named programmatically, and there may be issues with them overwriting variables that had already been saved.
See the attached "saveBrushDataCallback.m" file for an example of the use of this callback.
Some of the issues could be mitigated by using a dialog box, allowing the user to choose whether or not to save the selection, and also enter variable name(s) there.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by