Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 960
Nick Avatar asked Dec 23 '10 21:12

Nick


People also ask

How do you break a for loop in MATLAB?

The break statement exits a for or while loop completely. To skip the rest of the instructions in the loop and begin the next iteration, use a continue statement. break is not defined outside a for or while loop. To exit a function, use return .

How do you stop a running loop in MATLAB?

when an loop is running ctrl + c (just ctrl and c ) will exit any loop..

How do you do a break in MATLAB?

Alternatively, you can press the F12 key to set a breakpoint at the current line. If you attempt to set a breakpoint at a line that is not executable, such as a comment or a blank line, MATLAB sets it at the next executable line.

How do I stop an infinite loop from running MATLAB?

To stop execution of a MATLAB® command, press Ctrl+C or Ctrl+Break.


2 Answers

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
like image 104
gnovice Avatar answered Oct 15 '22 23:10

gnovice


I see two potential problems here.

First: variable handles is not a reference, setting handles.stop_now=1; is going "lost" after control flow exits pushbutton_Callback. Use guidata or other approaches to store and retrieve data.

Second problem: Use function drawnow(). See this article of Yair Altman for good explanation.

Summary: MATLAB graphics is Java Swing and IO operations (like pressing a button) happen on a special thread - Event Dispatch Thread (EDT). Calling drawnow(); flushes event queue and updates figure window.

like image 30
Mikhail Poda Avatar answered Oct 16 '22 01:10

Mikhail Poda