I'm trying to save brushed data to a variable on button click. I've read other questions but can't find the method to do it.
Inside a script the following code works:
t=0:0.2:25;
x=sin(t);
n=plot(t,x,'s');
brush on
pause
brushedData = find(get(n,'BrushData'));
However, calling the function selectBrush
does not work:
function selectBrush()
% Create data
t=0:0.2:25;
x=sin(t);
% Create figure with points
fig=figure();
n=plot(t,x,'s');
brush on;
addBP = uicontrol(1,'Style', 'pushbutton',...
'String', 'Get selected points index',...
'Position',[5, 5, 200, 30],...
'Units','pixel',...
'Callback',@()assignin('caller','selectedPoints',get(n,'BrushData')));
% ---> Now the user should select the points and click the button 'Get
% selected points index'
waitfor(fig)
% Display index of selected points once the figure is closed
disp(selectedPoints);
end
The error message I become is
Error using selectBrush>@()assignin('caller','selectedPoints',get(n,'BrushData'))
Too many input arguments.
I've tried other things like using eval('selectedPoints=,get(n,''BrushData'')')
as callback-function, using handles or defining separately a new callback function, everything without success.
How should I do it?
EDIT 1
excaza's method seems to work, but the callback function is only executed on the original value of the variable I'm redefining and not on the updated value.
With the following code,
function testcode()
% Create data
t = 0:0.2:25;
x = sin(t);
% Create figure with points
myfig = figure();
n = plot(t, x, 's');
brush on;
pointslist=[];
uicontrol('Parent', myfig, ...
'Style', 'pushbutton',...
'String', 'Get selected points index',...
'Position', [5, 5, 200, 30],...
'Units', 'pixels',...
'Callback', {@mycallback, n, pointslist} ...
);
% ---> Now the user should select the points and click the button 'Get
% selected points index'
waitfor(myfig)
% Display index of selected points once the figure is closed
disp(pointslist);
end
function mycallback(~, ~, mylineseries, pointslist)
% Ignore the first 2 function inputs: handle of invoking object & event
% data
assignin('caller', 'pointslist', [pointslist find(get(mylineseries,'BrushData'))])
end
if I push the button more than once before closing I'd expect having saved the points as many times as I pushed the button, not only the last button push.