How to Stop Fmincon from GUI

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

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

Dear Geoff, Thanks for your answer so far. However, the optimoptions seem to work a bit different still. My Code:
fmincon_options = optimoptions(@fmincon,'MaxIter',MaxIter,'TolX',TolX,...
'TolFun',TolFun, 'MaxFunEvals',MaxFunEvals,...
'Algorithm', 'interior-point','OutputFcn',{@functions.output_function.output_statusbar,hObject});
Normally, this adding brackets and a new parameter works fine like in custom data cursor functions. However, here I get the error:
Error using createCellArrayOfFunctions (line 49)
The field 'OutputFcn' must contain a function handle.
Error in barrier (line 101)
Error in fmincon (line 818)
[X,FVAL,EXITFLAG,OUTPUT,LAMBDA,GRAD,HESSIAN] =
barrier(funfcn,X,A,B,Aeq,Beq,l,u,confcn,options.HessFcn, ...
Is there any other syntax for passing another parameter you know of? Thanks a bunch Ravi
Ravi - I don't understand what you are attempting with
functions.output_function.output_statusbar
Is this a function? What version of MATLAB are you using?
Geoff,
This basically is only the path to the output function. As I have quite a lot of functions I sort them in folder with a plus in front to access them.
I contacted MathWorks now as well and they gave me the one liner that did the job.
drawnow -.-
So using their example with getappdata I solved it as followed. (if everybody else ever runs into that problem)
function stop_all_fit_Callback(hObject, eventdata, handles)
if get(handles.stop_all_fit, 'Value')==1
setappdata(0,'Stop_Fmincon', true);
elseif get(handles.stop_all_fit, 'Value')==0
setappdata(0,'Stop_Fmincon', false);
end
And basically my OutputFcn looks like this.
function stop = output_statusbar(~, optimValues, ~)
drawnow;
stop = getappdata(0,'Stop_Fmincon');
end
Thanks a bunch for your help as well Geoff. Ravi
a que se refiere el string
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.
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)

Community Treasure Hunt

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

Start Hunting!