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 ?

Looking for reliable MATLAB assignment help? Our expert MATLAB tutors deliver high-quality, easy-to-understand solutions tailored to your academic needs. Whether you're studying at Monash University, the University of Sydney, UNSW, or the University of Melbourne, we provide trusted MATLAB assistance to help you excel. Contact us today for the best MATLAB solutions online and achieve academic success!

MATLAB Assignment Help Services

Personalized Tutoring: Get one-on-one guidance from our MATLAB experts. Whether you're tackling basic concepts or advanced algorithms, we provide clear, step-by-step explanations to help you master MATLAB with confidence.

Assignment Assistance: Struggling with tight deadlines or complex assignments? Our team offers end-to-end support, from problem analysis to code development and debugging, ensuring your assignments meet the highest academic standards.

Project Development: Need expert help with your MATLAB research project? We assist in designing and implementing robust solutions, covering project planning, data collection, coding, simulation, and result analysis.

Coursework Support: Enhance your understanding of MATLAB with our comprehensive coursework assistance. We help you grasp lecture concepts, complete lab exercises, and prepare effectively for exams.

Thesis and Dissertation Guidance: Incorporate MATLAB seamlessly into your thesis or dissertation. Our experts provide support for data analysis, modeling, and simulation, ensuring your research is methodologically sound and impactful.

Contact us on WhatsApp for MATLAB help

Contact us on Telegram for MATLAB solutions