Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
832 views
in Technique[技术] by (71.8m points)

user interface - Breaking from for loop in MATLAB GUI

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...