MATLAB: Create a callback that waits for the brush to include some data and then display them

brushcallbackgather data

I have the following simple example:
x=-4:0.1:4;
y= x.^2-1;
f=figure;
h=plot( x, y );
bO = brush(gcf);
set(bO,'enable','on');
data=logical(get(h,'brushdata'));
disp(data)
If I run it all the data will be zero. If I run it until the set command, choose subsequently the data from the plot and then continue with executing the other two commands the selected data will be displayed.
How can I control with the callback to wait until I gather the data and then display them?

Best Answer

After trying a lot I and taking into account a gui that I had done in the past I managed to do more or less what I wanted.
Here is the script in which I plot the data and from which I want to extract some brushed data:
x=-4:0.1:4;
y= x.^2-1;
f=figure;
h=plot( x, y );
STR={'Press close button when finishing with selection'};
out=brushpanel(STR,f);
and here is the brushpanel function that I made. It returns the selected data from the brush and enables off the brush.
function out=brushpanel(STR,fighandle)
g.fh = figure('units','pixels',...
'position',[1169 324 380 100],...
'menubar','none',...
'Color','w',...
'name','Select plot regions',...
'numbertitle','off',...
'resize','off');
g.sub=annotation('textbox',...
'units','pixels',...
'position',[0 0 380 100],...
'Tag','box',...
'Color',[58 51 153]/255,...
'BackgroundColor','none',...
'String',STR,...
'Fontsize',14,...
'FaceAlpha',0,...
'EdgeColor', [112 112 112]/255);
g.bg = uibuttongroup('units','pix',...
'pos',[0 0 380 50],...
'BorderType','line',...
'ForegroundColor','w');
g.rd = uicontrol(g.bg,...
'style','push',...
'unit','pix',...
'position',[260 15 70 22],...
'Fontsize',8,...
'string','Close');
g.bO = brush(fighandle);
g.axhandle=get(fighandle,'CurrentAxes');
set(g.bO,'enable','on');
set(g.rd(:),'callback',{@select,g});
uiwait(g.fh)
hBrushLine = findall(g.axhandle,'tag','Brushing');
brushedData = get(hBrushLine, {'Xdata','Ydata'});
brushedIdx = ~isnan(brushedData{1});
brushedXData = brushedData{1}(brushedIdx);
brushedYData = brushedData{2}(brushedIdx);
out=[brushedXData' brushedYData'];
close(g.fh)
function select(varargin)
g = varargin{3};
str=get(g.rd(1),'val');
if str(1)==1
g.st=true;
set(g.bO,'enable','off');
end
guidata(g.fh,g.bO);
uiresume
I would appreciate the contributors' useful comments. The answer that I provided above covers my needs to a certain extend. I just posted it here with the intention of providing a solution to other people that might have a similar question as me. Thanks