在 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?
附注.我不希望 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
中,初始化一个名为 example 的标志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,它开始在 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
按下停止按钮后的截图:
Screenshot after pressing the STOP button:
希望有帮助!
这篇关于在 MATLAB 进程中间停止 GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!