Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
546 views
in Technique[技术] by (71.8m points)

user interface - Stop A GUI in a middle of process in MATLAB

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. 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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:

enter image description here

Hope that helps!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...