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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…