Ask an expert. Trust the answer.

Your academic and career questions answered by verified experts

Stop A GUI in a middle of process in MATLAB

Date: 2022-12-20 13:32:51

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. 

Expert Answer:

s: 

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: 

Hope that helps!

Why Matlabhelpers ?

Our Matlab assignment helpers for online MATLAB assignment help service take utmost care of your assignments by keeping the codes simple yet of high-quality. We offer the most reliable MATLAB solutions to students pursuing their Computer Science course from the Monash University, the University of Sydney, the University of New South Wales, the University of Melbourne; to name a few. Approach us today for best Matlab solutions online!

Our Comprehensive Matlab Assignment Help Services

Personalized Tutoring:Our team of MATLAB experts offers one-on-one tutoring sessions tailored to your specific needs. Whether you’re struggling with basic concepts or advanced algorithms, we provide clear, step-by-step guidance to help you understand and master MATLAB.

Assignment Assistance:Facing tight deadlines or complex assignments? We’re here to help! From initial problem analysis to code development and debugging, we offer full-spectrum support to ensure your assignments meet the highest standards.

Project Development: Need help with a research project? Our specialists can assist you in designing and implementing robust MATLAB solutions. We cover everything from project planning and data collection to coding, simulation, and result analysis.

Coursework Support: We provide comprehensive support for your coursework, helping you understand lectures, complete lab exercises, and prepare for exams. Our goal is to ensure you grasp the core principles and practical applications of MATLAB.

Thesis and Dissertation Guidance:Writing a thesis or dissertation? Our experts can assist you in incorporating MATLAB for data analysis, modeling, and simulation. We help you develop a strong methodological framework and ensure your research stands out.

whatsApp order on matlabhelpers.com

telegram order on matlabsolutions.com