How to Stop Fmincon from GUI

6 views (last 30 days)
Ravi Goyal
Ravi Goyal on 10 Dec 2015
Commented: Kevin Thümmler on 17 Mar 2021
How to stop fmincon from a pushbutton in a Matlab Gui?
The explaination posted here at the botton does not work, and can not work, as hObject never is passed into the outputfunction
function stop = outfun(x,optimValues,state)
stop = false;
% Check if user has requested to stop the optimization.
stop = getappdata(hObject,'optimstop');
Is there a way to stop the execution of the fmincon algorithm from a Pushbutton? Thanks a lot. Ravi

Accepted Answer

Geoff Hayes
Geoff Hayes on 10 Dec 2015
Ravi - I suspect that the example is only meant to provide an idea of how to do this given that you could have created your GUI either using GUIDE or using the programmatic approach. If you are using GUIDE, then you can always pass in hObject as an additional input to the outfun. For example, suppose that you have a start push button to start the optimization where you create the options (for fmincon) and start the optimization. For example,
function start_pushbutton(hObject, eventdata, handles)
% do some stuff
% create the options
opts = optimoptions(@fmincon,'OutputFcn','{@outfun,hObject});
% start the optimization
Then, you would change the function signature of your outfun to be
function stop = outfun(x,optimValues,state,hObject)
Here, it is assumed that you have created the outfun in the same m-file as for your GUI. Now rather than using getappdata (which forces you to have called setappdata to update hObject with the state of stop), I would use guidata to get the handles structure as
function stop = outfun(x,optimValues,state,hObject)
stop = false;
handles = guidata(hObject);
if isfield(handles,'optimstop')
stop = handles.optimstop;
end
And so your stop button callback would look something like
function stop_pushbutton(hObject, eventdata, handles)
handles.optimstop = true;
guidata(hObject,handles);
In the above we set the optimstop field and then save that change to the handles structure.
I haven't tried the above (since I don't have the Optimization Toolbox) but I think that it can be made to work for your implementation.
  6 Comments
Pappu Murthy
Pappu Murthy on 4 Feb 2017
This solution didn't work at all for me. Pushing the stop button is not able to interupt the process and update the stop value at all. so the optimization process keeps continuing to the end.
Kevin Thümmler
Kevin Thümmler on 17 Mar 2021
There are intermediate execution during the optimization process. You can visualize it when you plot the number of times calling the output function and the number of times calling the objective function or you find it in the doc as well. I had the same problem. My solution was to set the error to 0 directly in the objective function, so that the optimizer thinks the job is done and stops the process

Sign in to comment.

More Answers (0)

Categories

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!