我在MATLAB的开放功能中有一个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);
由于某种原因,尽管使用句柄定义变量,但循环在按下按钮时并未中断。 有人知道发生了什么吗? 谢谢。
handles = guidata(hObject);
if handles.stop_now==1 break; end handles.H{i} = imfreehand; handles.P{i}= handles.H{i}.createMask;
end问题是,一旦我调用pushbutton,我只能在选择一个额外的自由手区域后退出循环。我该如何解决这个问题? - Nickbreak
会退出循环并将控制流传递到imfreehand()
。因此,你应该使用return
而不是break
。 - Mikhail Poda