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 个评论
stephen brewis
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
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 个评论
Walter Roberson
Walter Roberson 2012-1-9
As I wrote above, "for any one object that has been brushed"
Ross
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
h = brush;
set(h,'Color',[1 0 0],'Enable','on','ActionPostCallback',@GetSelectedData);
Handle = findobj(gcf,'-property','BrushData');
brushed_locs = find(get(Handle, 'BrushData'));
When i use like this it gives me an error like,
Undefined function 'find' for input arguments of type 'cell'.
Error in nakamura (line 94)
brushed_locs = find(get(Handle, 'BrushData'));

Rifat Tolga KIRAN
And if also fire the @GetSelectedData function in the postcallback, even if i write simple fprintf in the function i get following errors:
Warning: An error occurred during the mode callback.
> In uitools.uimode.fireActionPostCallback at 14
In datamanager.brushup at 114
In brush>@(es,ed)datamanager.brushup(es,ed)
In hgfeval at 63
In uitools.uimode.modeWindowButtonUpFcn at 46
In uitools.uimode.modeControl>localModeWindowButtonUpFcn at 155
  1 个评论
Walter Roberson
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
Rifat Tolga KIRAN 2012-1-11
Here is my GetSelectedData:
function [SelectedData] = GetSelectedData(es,ed)
clc;
fprintf('Obje üzerinden veri seçimi tamamlandı!\n');
Handle = findobj(gcf,'-property','BrushData');
a = get(Handle,'BrushData')
When i add two variables into the input segment the errors has gone but now i have a problem with variables that are not global to usage. I cant see the SelectedData variable in the main program.
  2 个评论
Walter Roberson
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
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 .

请先登录,再进行评论。


Rifat Tolga KIRAN
Rifat Tolga KIRAN 2012-1-12
i have tried to find every handles' properties including 'BrushData' but none of them has a tag like this. But my function returns data which is fulled with zeros and one of which shows the selected and nonselected indices. let me share the program and maybe you can help me by that way.

Jesse Salazar
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.

Udo Schröder
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
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 CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by