How can i save the brushed data to a variable?
38 views (last 30 days)
Show older comments
I am working on a program which includes brushing datas on a plot. Supposing that we have a script like this:
clear;clc;close;
plot(randn(100,1));
h = brush;
set(h,'Enable','on','ActionPostCallback',@GetSelectedData);
what kind of a function should i write to retrieve 'GetSeletedData'?
and i want to get the selected data in a variable. What should i do? i am researhing this issue for 2 days but i couldnt catch a clue. Thank you for your interests.
2 Comments
Walter Roberson
on 8 Jan 2012
Edited: Walter Roberson
on 14 Nov 2018
See also Rifat's previous thread http://www.mathworks.com/matlabcentral/answers/25488-brush-data-can-not-be-obtained
stephen brewis
on 24 Dec 2018
I am not a programmer but hope you find this usefull. !!! Make sure you BRUSH the same number of points from both plots
Stephen
function brushTest()
figure();
x=1:10;
plot(x.^2);
hold on
plot(1:10);
h=brush;
set(h,'Enable','on','ActionPostCallback',@brushedDataCallback);
function brushedDataCallback(~,~)
h=findobj(gca,'type','line')
clear lineBrushed
for i=1:size(h)
idx=get(h(i),'BrushData');
idx=logical(idx);
x=get(h(i),'XData');
x=x(idx);
idx=logical(idx);
y=get(h(i),'YData');
y=y(idx);
lineBrushed(i,1,:)=x(1,:);
lineBrushed(i,2,:)=y(1,:);
end
clear x
x(1,:)=lineBrushed(1,1,:)
y(1,:)=lineBrushed(1,2,:)
x(1,:)=lineBrushed(2,1,:)
y(1,:)=lineBrushed(2,2,:)
Answers (7)
Walter Roberson
on 8 Jan 2012
In your previous thread, I gave you a link to a routine that find the brushed objects.
As hinted in the code referenced, for any one object that has been brushed, get() the Brushdata property; it will be a logical vector, where true indicates that the corresponding datapoint has been brushed. (Or so it appears to me.)
xd = get(Handle, 'XData');
yd = get(Handle, 'YData');
brush = get(Handle, 'BrushData');
brushed_x = xd(brush);
brushed_y = yd(brush);
Or if you prefer indices,
brushed_locs = find(get(Handle, 'BrushData'));
3 Comments
Ross
on 21 Oct 2015
to answer Rifat Tolga KIRAN by slightly changing the above example for when using from the matlab console:
ax1 = plot(rand(50,1));
%then select the brushing tool either with a console command or via the figure menu, select your data.
I added conversion to logical datatype so that it can be used to as an index for the xd and yd arrays.
brush = logical(get(ax1, 'BrushData'));
xd = get(ax1, 'XData');
yd = get(ax1, 'YData');
brushed_x = xd(brush);
brushed_y = yd(brush);
Rifat Tolga KIRAN
on 9 Jan 2012
1 Comment
Walter Roberson
on 9 Jan 2012
brushed_locs = cellfun(@find, get(Handle, 'BrushData'), 'Uniform', 0);
Rifat Tolga KIRAN
on 9 Jan 2012
1 Comment
Walter Roberson
on 9 Jan 2012
What is your function declaration for GetSelectedData ? If it does not have two arguments, it would fail.
Rifat Tolga KIRAN
on 11 Jan 2012
2 Comments
Walter Roberson
on 11 Jan 2012
There are very few kinds of callbacks that can return values, such as ButtonDownFilter for zoom mode. ActionPostCallback cannot return values.
http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.3F
Note: in checking around, I find that R2009a and R2009b did not allow using ActionPreCallback or ActionPostCallback; those were fixed in R2010a. The workaround is shown at
http://www.mathworks.com/support/solutions/en/data/1-6AVMMS/index.html?product=SL&solution=1-6AVMMS
Walter Roberson
on 11 Jan 2012
Also, remember that your findobj() is going to be returning the handles of everything in your figure that has the BrushData property, and when you get() on multiple handles then a cell array will be returned by get() .
Your code shows assigning to "a" but does not show you creating SelectedData .
Jesse Salazar
on 14 Jan 2015
You want to execute the following:
hBrushing = findall(gca,'tag','Brushing');
brushData = get(hBrushing, {'Xdata','Ydata'});
brushIdx = ~isnan(brushData{1}); % Puts a '1' at indices that aren't NAN
brushXData = brushData{1}(brushIdx);
brushYData = brushData{2}(brushIdx);
Note that gca can be replaced with the handle to your axes, such as:
hFig = figure; hAxes = axes(hFig);
And if you need the vector indices of the brushData within the line:
brushVecIdx = find(brushIdx)
Not sure I have seen the "BrushData" tag, mentioned in a previous answer.
0 Comments
Udo Schröder
on 14 Nov 2018
I think the problem is up to now not solved. So I have the same issue.
I want to use the Brush/Select Data tool from a plot window to mark data. This data (or index) should then be automatically transferred into a variable in the workspace.
The figure object does not have any property like "BrushData" or something similar. I am working with R2018b.
Can anyone help?
1 Comment
Walter Roberson
on 15 Nov 2018
Figure objects are not brushable. Some kinds of individual graphics objects are brushable.
Unfortunately,
findobj(gcf,'-property','BrushData')
no longer works in HG2. BrushData is an undocumented property, and apparently now you cannot findobj() -property with an undocumented property. The most efficient workaround I know at the moment is
figure_handles = findall(gcf); Handles = figure_handles( arrayfun(@(H) isprop(H, 'BrushData'), figure_handles) );
after which Handles contains the handles of all of the potentially brushable objects. After that you can do something like
info = [num2cell(Handles),arrayfun(@(H) find(H.BrushData), Handles, 'uniform', 0)];
This will give you a cell array in which the first column is graphic objects and the second column is a vector of corresponding brushed indices. You cannot simply use a vector of indices because it is common to have more than one brushable graphics object in a plot and you need to have context for the indices.
See Also
Categories
Find more on Graphics Object Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!