How can i save the brushed data to a variable?
38 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2012-1-8
编辑:Walter Roberson
2018-11-14
See also Rifat's previous thread http://www.mathworks.com/matlabcentral/answers/25488-brush-data-can-not-be-obtained
stephen brewis
2018-12-24
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,:)
回答(7 个)
Walter Roberson
2012-1-8
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 个评论
Ross
2015-10-21
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
2012-1-9
1 个评论
Walter Roberson
2012-1-9
What is your function declaration for GetSelectedData ? If it does not have two arguments, it would fail.
Rifat Tolga KIRAN
2012-1-11
2 个评论
Walter Roberson
2012-1-11
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
2012-1-11
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
2015-1-14
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 个评论
Udo Schröder
2018-11-14
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 个评论
Walter Roberson
2018-11-15
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!