![]() |
在MATLAB GUI中打破for循环
我在MATLAB中的GUI的打开函数中有一个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); 出于某种原因,尽管使用句柄定义了变量,但在按下按钮时循环不会中断。有人知道发生了什么吗?谢谢。 [B]回答:[/B] 您遇到的问题是传递给handles的[URL="http://www.mathworks.com/help/techdoc/creating_guis/f10-998580.html#f10-999687"]打开函数[/URL]的值的结构固定为调用打开函数时的值。您永远不会检索通过pushbutton_Callback更新的新结构。您可以通过在循环中调用[URL="http://www.mathworks.com/help/techdoc/ref/guidata.html"]GUIDATA[/URL]来检索新结构。这是我建议您尝试编写循环的方法: 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 [B]较大的GUI设计问题... [/B] 基于您的评论中关于您要使用GUI完成的内容的其他描述,我认为可能会有更好的设计方法。您不必为用户重复输入ROI而产生连续的循环,而是必须按一个按钮才能停止,而是可以取消循环和停止按钮,并在GUI中添加“添加ROI”按钮。这样,用户在要添加另一个ROI时只需按下按钮即可。您可以首先使用以下初始化替换打开函数中的for循环: handles.nROIs = 0; %# Current number of ROIs handles.H = {}; %# ROI handles handles.P = {}; %# ROI masks guidata(hObject,handles); %# Update the GUI data 然后,您可以使用以下内容替换按钮的回调: 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 [url=https://stackoverflow.com/questions/4522447]更多&回答...[/url] |
所有时间均为北京时间。现在的时间是 03:05。 |
Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.