在MATLAB的中间过程中停止GUI [英] Stop A GUI in a middle of process in MATLAB
问题描述
我正在使用MATLAB R2014b中的GUIDE
设计GUI.我的程序有一个很长的循环(需要2到5个小时来处理).我想在我的GUI中有一个按钮,以便用户每次想要时都停止该过程(GUI会根据循环结果不断更新图形和文本).在结束循环后不按Control+C
之类的操作.我该如何实施?
I'm designing a GUI using GUIDE
in MATLAB R2014b. My program has a long loop (takes 2~5h to process). I want have a button in my GUI that the user stop the process every time he/she want (The GUI is updating graphs and texts continuously based on result of loops). Something like pressing Control+C
not after ending a loop. How can I implement this?
PS. 我不希望MATLAB删除我的工作区.通过更改GUI中的某些选项,用户可以使用先前加载的数据和工作区继续该过程.
PS. I don't want MATLAB remove my workspace. The user can continue the process with previous loaded data and workspace by changing some options in GUI.
推荐答案
这是一个应该起作用的技巧:在GUI的某个位置(例如在其OpeningFcn
中),将名为StopNow
的标志初始化为false
并将其存储在GUI的句柄结构中.然后,在需要很长时间才能执行的循环中,只要将标志设置为true
,就放置一个if
语句并调用return
.这将停止循环的执行,您将可以访问数据.您可以按下按钮来更改标志值.
Here is a trick that should work: Somewhere in the GUI, like in its OpeningFcn
for instance, initialize a flag named for example StopNow
to false
and store it in the handles structure of the GUI. Then in the loop that takes long to execute, put an if
statement with a call to return
whenever the flag is set to true
. That will stop the execution of the loop and you will have access to your data. You can make a pushbutton to change the flag value.
示例代码:我制作了一个简单的GUI,该GUI开始枚举for循环中的数字并将其打印在文本框中.当您按下STOP按钮时,该标志设置为true,并且循环停止.如果不清楚,请告诉我.
Sample code: I made a simple GUI that starts enumerating digits in a for loop and printing them in a text box. When you press the STOP button, the flag is set to true and the loop stops. If something is unclear please tell me.
function StopGUI
clear
clc
close all
%// Create figure and uielements
handles.fig = figure('Position',[440 500 400 150]);
handles.CalcButton = uicontrol('Style','Pushbutton','Position',[60 70 80 40],'String','Calculate','Callback',@CalculateCallback);
handles.StopButton = uicontrol('Style','Pushbutton','Position',[250 70 80 40],'String','STOP','Callback',@StopCallback);
%// Initialize flag
handles.StopNow = false;
handles.Val1Text = uicontrol('Style','Text','Position',[150 100 60 20],'String','Value 1');
handles.Val1Edit = uicontrol('Style','Edit','Position',[150 70 60 20],'String','');
guidata(handles.fig,handles); %// Save handles structure of GUI. IMPORTANT
function CalculateCallback(~,~)
%// Retrieve elements from handles structure.
handles = guidata(handles.fig);
for k = 1:1000
if handles.StopNow == false
set(handles.Val1Edit,'String',num2str(k));
pause(.5) %// The pause is just so we see the numbers changing in the text box.
else
msgbox('Process stopped');
return
end
end
guidata(handles.fig,handles); %// Save handles structure of GUI.
end
function StopCallback(~,~)
%// Retrieve elements from handles structure.
handles = guidata(handles.fig);
handles.StopNow = true;
guidata(handles.fig,handles); %// Save handles structure of GUI.
end
end
按下STOP按钮后的屏幕截图:
Screenshot after pressing the STOP button:
希望有帮助!
这篇关于在MATLAB的中间过程中停止GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!