Clear Filters
Clear Filters

Using Stop button in GUI and Optimization

15 views (last 30 days)
I developed a GUI and I have included a Stop button and its callback I have declared a global variable "StopOptim" and gave it a value 1; My idea is during the call to OutputFcn... I should be able to turn the logical variable stop to true if the button is pressed so that my optimization process stop gracefully and I recover whatever info i get thust far. for ex. i have maxIter =100 and i see things are not moving much by iter =20 then i would like to change the stop to true. so i press the gui stop button to change the initially set value of false to StopOptim variable to true and check that at the end of each OutputFcN. since the code is long I just give the relevant portions only here:
function stop=ViOptResults(x,optimValues,state)
global history VarScale Var Act
global StopOptim handles
stop = false;
so basically if the push button is pressed StopOptim turns the initial vaule of false into true for stop which is used by Optimization process to stop at that iteration gracefully. However, my problem is no matter how many times and how hard i try clicking on stop button, it does not seem to work and StopOptim is always remaining as false and the execution keeps continuing as if nothing happened. Here is my Stop button code.
function OptimStop_Callback(hObject, eventdata, handles) --- global StopOptim
handles.OptimStop= 1;
guidata(hObject, handles); % Update handles structure
I have been struggling with this quite some time now. I am sure it is a simple thing but somehow I am missing. Please help. Thanks in advance.

Accepted Answer

Carl on 6 Feb 2017
Edited: Carl on 6 Feb 2017
I understand that you have a long running optimization function. You would like to preemptively stop this process by clicking a button in your UI. You're trying to do this by setting a global variable in the button callback, and checking the value of the variable in the optimization code.
The reason this doesn't work is that the button callback gets queued behind the optimization process. Since they are running on the same thread, MATLAB will wait until the first function finishes before running the second function. So even if you click the button during the optimization, the global variable is not being set until the first function finishes.
You can, however, interrupt a callback's execution. See the link below:
The documentation says "When an object's Interruptible property is set to 'on', its callback can be interrupted at the next occurrence of one of these commands: drawnow, figure, getframe, waitfor, or pause." So it seems like you need one of these commands in your optimization code, maybe a 'pause' would work. Look at the page I linked for a good example on using interrupts.
  1 Comment
Pappu Murthy
Pappu Murthy on 6 Feb 2017
Thanks for the help. Your suggestion worked fine. I had to turn the interuptible variable 'on' and everything works fine there on.

Sign in to comment.

More Answers (0)


Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!