按下按钮时将刷过的数据保存到变量

Save brushed data to variable on button push

我试图在单击按钮时将刷过的数据保存到一个变量中。我已阅读 other questions 但找不到执行此操作的方法。

在脚本中,以下代码有效:

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

如果我在关闭前多次按下按钮,我希望在按下按钮时保存点数,而不仅仅是最后一次按下按钮。

the documentation开始,MATLAB的回调默认总是发送2个变量:

  • The handle of the object whose callback is executing. Use this handle within your callback function to refer to the callback object.

  • The event data structure, which can be empty for some callbacks or contain specific information that is described in the property description for that object.

所以这里发生的事情是 assignin 调用被传递的变量多于它可以处理的 2 个变量,这就是它抛出错误的原因(我建议在您的问题中包含错误消息)。

要立即解决问题,您可以使用文档中提到的元胞数组表示法来创建本地回调函数:

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;
uicontrol('Parent', myfig, ...
          'Style', 'pushbutton',...
          'String', 'Get selected points index',...
          'Position', [5, 5, 200, 30],...
          'Units', 'pixels',...
          'Callback', {@mycallback, n} ...
           );

% ---> 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(selectedPoints);
end

function mycallback(~, ~, mylineseries)
% Ignore the first 2 function inputs: handle of invoking object & event
% data

assignin('caller', 'selectedPoints', get(mylineseries,'BrushData'))
end

应该可以正常运行。另请注意适当的 assignin 语法,它在您的示例中不正确。