在MATLAB GUI中打破for循环 [英] Breaking from for loop in MATLAB GUI

查看:519
本文介绍了在MATLAB GUI中打破for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a for loop in the opening function of a GUI in MATLAB and I'm trying to use a callback button to break the loop. I'm new to MATLAB. Here's the code I have:

%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);

由于某种原因,尽管使用句柄定义了变量,但按下按钮时循环不会中断.有人知道发生了什么吗?谢谢.

For some reason, despite defining the variables with handles, the loop doesn't break upon pressing the button. Anyone know what's going on? Thanks.

推荐答案

您遇到的问题是传递给 GUIDATA 来检索新结构.这是我建议您尝试编写循环的方法:

The problem you are having is that the structure of values passed to the opening function for handles is fixed at whatever it was when the opening function was called. You never retrieve the new structure that is updated by pushbutton_Callback. You can retrieve the new structure by calling GUIDATA in your loop. Here's how I would suggest you try writing your loop:

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进行的操作的其他描述,我认为可能会有更好的设计方法.不必让用户重复输入ROI,而必须连续按一个按钮才能停止,就可以取消循环和停止按钮,并在GUI中添加添加ROI"按钮.这样,用户在要添加另一个ROI时只需按下按钮即可.您可以先使用以下初始化来替换打开函数中的for循环:

The larger GUI design issue...

Based on the additional description in your comment about what you are trying to accomplish with your GUI, I think there may be a better way to design it. Instead of having a continuous loop for the user to repeatedly enter ROIs, which they then have to press a button to stop, you can do away with the loop and the stop button and add an "Add an ROI" button to your GUI. This way, the user can just press a button when they want to add another ROI. You can first replace the for loop in the opening function with the following initializations:

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

然后,您可以使用以下内容替换按钮的回调:

Then you can replace the callback for your button with something like the following:

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

这篇关于在MATLAB GUI中打破for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆