Interrupting an optimization with a GUI
1 view (last 30 days)
Show older comments
So this is a bit of a two part question. Part of my program involves using fminsearch to calculate values such that the RMS residual using those values is as small as possible for my system. Since the models can become very complex, the optimizations in some cases can be up to 15 minutes long. In the past, the values being used in the current iteration of fminsearch were displayed in the command window so the user could terminate the optimization early if he/she noticed something going wrong by pressing control-c. However, since I am compiling this program into an executable, I have neither the use of the command window to display the current values nor the ability to terminate the optimization with control-c. Instead, I am displaying the current values to a text box in the GUI.
This brings me to my first question: right now the code to set the string of the textbox is placed inside the output function of fminsearch so that the text box is updated every iteration (well, every 10 iterations since every iteration would be excessive). I could not figure out how to pass the handles structure to the output function without causing an error in the optimization so I have currently settled for storing the handles structure to a global variable and then invoking the global variable inside the output function. This works and the text box displays what I want it too but I am wondering if there is a better method to pass the handles structure to the output function without using a global variable or if there is another method to display the values used mid-optimization in the GUI.
My second question involves actually interrupting the optimization. After searching in the Matlab documentation I found this:
---
Stopping an Optimization Based on GUI Input. If you design a GUI to perform optimizations, you can make the output function stop an optimization when a user clicks a Stop button on the GUI. The following code shows how to do this, assuming that the Stop button callback stores the value true in the optimstop field of a handles structure called hObject:
function stop = outfun(x,optimValues,state)
stop = false;
% Check if user has requested to stop the optimization.
stop = getappdata(hObject,'optimstop');
---
As I understand this, I need to create a push button in the GUI. Then, under the button's callback, I add the code
setappdata(handles.optimizestop,'optimstop',true)
and then in the output function I add
stop=getappdata(handles.optimizestop,'optimstop')
However, I have tried this and the optimization just continues to completion.
I am still somewhat new to Matlab so I apologize if I am missing something obvious but any help would be greatly appreciated.
2 Comments
Walter Roberson
on 31 Jul 2012
You might want to add a disp() or the like inside the stop function, in order to determine whether the stop function is being called.
Answers (0)
See Also
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!