0

我正在尝试在单击按钮时将刷过的数据保存到变量中。我已经阅读了其他问题,但找不到执行此操作的方法。

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

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

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

4

1 回答 1

1

文档中,默认情况下,MATLAB 的回调总是发送 2 个变量:

  • 正在执行其回调的对象的句柄。在回调函数中使用此句柄来引用回调对象。

  • 事件数据结构,对于某些回调可以为空,或者包含在该对象的属性描述中描述的特定信息。

所以这里发生的事情是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语法,在您的示例中不正确。

于 2016-06-06T12:10:13.053 回答