Nic*_*ick 6 matlab user-interface
我for在MATLAB的GUI的打开功能中有一个循环,我正在尝试使用回调按钮来打破循环.我是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);
Run Code Online (Sandbox Code Playgroud)
出于某种原因,尽管使用句柄定义变量,但按下按钮时循环不会中断.有谁知道发生了什么?谢谢.
您遇到的问题是价值观的结构传递到开启功能的handles固定在任何它是开放函数被调用时.您永远不会检索由更新的新结构pushbutton_Callback.您可以通过在循环中调用GUIDATA来检索新结构.以下是我建议您尝试编写循环的方法:
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
Run Code Online (Sandbox Code Playgroud)
根据评论中有关您要使用GUI完成的内容的其他说明,我认为可能有更好的方法来设计它.而不必为用户重复输入的ROI,他们则必须按一个按钮来停止连续循环的,你可以不设循环和停止按钮,添加一个"添加ROI"按钮,您的GUI.这样,用户可以在想要添加另一个ROI时按下按钮.您可以先使用以下初始化替换open函数中的for循环:
handles.nROIs = 0; %# Current number of ROIs
handles.H = {}; %# ROI handles
handles.P = {}; %# ROI masks
guidata(hObject,handles); %# Update the GUI data
Run Code Online (Sandbox Code Playgroud)
然后,您可以使用以下内容替换按钮的回调:
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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10904 次 |
| 最近记录: |