我正在尝试在单击按钮时将刷过的数据保存到变量中。我已经阅读了其他问题,但找不到执行此操作的方法。
在脚本中,以下代码有效:
t=0:0.2:25;
x=sin(t);
n=plot(t,x,'s');
brush on
pause
brushedData = find(get(n,'BrushData'));
但是,调用该函数selectBrush
不起作用:
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
我成为的错误信息是
Error using selectBrush>@()assignin('caller','selectedPoints',get(n,'BrushData'))
Too many input arguments.
我尝试过其他eval('selectedPoints=,get(n,''BrushData'')')
的事情,比如用作回调函数、使用句柄或单独定义一个新的回调函数,一切都没有成功。
我该怎么做?
编辑 1
excaza 的方法似乎有效,但回调函数仅对我正在重新定义的变量的原始值执行,而不是对更新的值执行。
使用以下代码,
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
如果我在关闭前多次按下按钮,我希望保存点的次数与按下按钮的次数一样多,而不仅仅是最后一次按下按钮。