user-interface - 在 MATLAB GUI 中中断 for 循环

标签 user-interface matlab

我在 MATLAB 的 GUI 的打开函数中有一个 for 循环,我正在尝试使用回调按钮来中断循环。我是 MATLAB 的新手。这是我的代码:

%In the opening function of the GUI
handles.stop_now = 0;
for i=1:inf
   if handles.stop_now==1
      break;
   end
end


% Executes on button press 
function pushbutton_Callback(hObject, eventdata, handles)
% hObject    handle to end_segmenting_button (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles.stop_now=1;
guidata(hObject, handles);

出于某种原因,尽管定义了带有句柄的变量,但循环并没有在按下按钮时中断。有人知道发生了什么事吗?谢谢。

最佳答案

您遇到的问题是传递给 opening function 的值的结构for handles 固定在调用打开函数时的任何位置。您永远不会检索由 pushbutton_Callback 更新的新结构。您可以通过调用 GUIDATA 来检索新结构在你的循环中。以下是我建议您尝试编写循环的方法:

handles.stop_now = 0;  %# Create stop_now in the handles structure
guidata(hObject,handles);  %# Update the GUI data
while ~(handles.stop_now)
  drawnow;  %# Give the button callback a chance to interrupt the opening function
  handles = guidata(hObject);  %# Get the newest GUI data
end

更大的 GUI 设计问题...

根据您评论中关于您尝试使用 GUI 完成的内容的附加描述,我认为可能有更好的设计方法。您可以取消循环和停止按钮,并在 GUI 中添加一个“添加 ROI”按钮,而不是让用户重复输入 ROI(然后他们必须按下按钮停止)的连续循环。这样,用户在想要添加另一个 ROI 时只需按一下按钮即可。您可以先用以下初始化替换打开函数中的 for 循环:

handles.nROIs = 0;  %# Current number of ROIs
handles.H = {};  %# ROI handles
handles.P = {};  %# ROI masks
guidata(hObject,handles);  %# Update the GUI data

然后您可以将按钮的回调替换为如下内容:

function pushbutton_Callback(hObject,eventdata,handles)
%# Callback for "Add new ROI" button
  nROIs = handles.nROIs+1;  %# Increment the number of ROIs
  hROI = imfreehand;  %# Add a new free-hand ROI
  position = wait(hROI);  %# Wait until the user is done with the ROI
  handles.nROIs = nROIs;  %# Update the number of ROIs
  handles.H{nROIs} = hROI;  %# Save the ROI handle
  handles.P{nROIs} = hROI.createMask;  %# Save the ROI mask
  guidata(hObject,handles);  %# Update the GUI data
end

关于user-interface - 在 MATLAB GUI 中中断 for 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4522447/

相关文章:

html - 仅使用 HTML+CSS 的适当子菜单

c++ - 视觉 C++ : Unable to invoke method from another class

css - tinymce 使用 twitter bootstrap 更改文本区域宽度

Matlab:在循环中绘制带有保持和保持关闭的子图,而不总是调用 xlabel、ylabel、xlim 等

matlab - Octave:高斯分布的 3D 曲面图

java - 用java制作一个登录窗口重定向到另一个GUI

java - 在 java swing 中定位按钮

arrays - 随机排列数组,同时间隔重复元素

matlab - 有没有办法在 MATLAB 中安全地修改 mex 文件?

matlab - 将终端下的Matlab不可见图保存为相同大小的图像