无限循环中的Matlab imrect

Matlab imrect in infinite loop

我有一个 Matlab UI,我希望用户在选择单选按钮后立即使用 imrect输入几个区域。
不知道会选择多少个区域,所以选择需要无限循环
一旦选择 另一个单选按钮,imrect 输入应该停止 ,我无法开始工作。

这是一个最小的工作示例:

function mwe
ax = axes('Position', [0 0 1 1]);
bg = uibuttongroup('Position',[0 0 .15 1], 'SelectionChangedFcn',{@bselection, ax});
r1 = uicontrol(bg, 'Style','radiobutton', 'String','Option 1', 'Position',[10 250 100 30]);
r2 = uicontrol(bg, 'Style','radiobutton', 'String','Option 2', 'Position',[10 225 100 30], 'Value',1);

function bselection(source, event, ax)
  switch event.NewValue.String
    case 'Option 1'
      while true
        h = imrect(ax);
        % do stuff
        delete(h);
      end
    case 'Option 2'
      % do not show imrect and do other stuff
  end

感谢任何帮助。

您可以在按钮上设置Interruptible property。您还可以将 BusyAction 设置为 cancel。帮助说:

The interruption occurs at the next point where MATLAB processes the queue, such as when there is a drawnow, uifigure, getframe, waitfor, or pause command.

因此,如果您包含 'pause',它可能不会停止,直到选择了下一个矩形。这是因为一旦您调用了 imrect,它可能不知道它必须停止。

但是,如果 imrect 阻止 matlab UI 触发回调,则此方法可能无效。

一个更好的方法是使用无限循环。您需要通过检查告诉它何时结束 --

running = true;
while running
  h=imrect(ax)
  % do stuff
  delete(h)
  if (SOMETHING)
      running = false
  end
end

什么东西?我们需要检查按钮是否被取消选择。

你可以使用

if r1.Value!=1
  running = false
end

这将检查 r1 是否未被选中,如果是,运行 变为假,循环停止循环。