how to get and plot current "x" and "fval" during an fmincon optimization embedded in a GUI

Hi,
I am writing an optimization code embedded in a GUI. Optimization works well at the moment, but I would like to improve the functionality of my GUI. In that context, I would like to plot a dynamic graph of "current point" and "current function value" of fmincon iterations on my GUI. My problems are:
1) I couldn't achieve to write an working Output function.
2) About GUI plot; I am not sure if I have to write plot command in my optimization function or in GUI callback
Thanks in advance
Sercan

 Accepted Answer

  1. I don't know what the problem was with the OutputFcn, but this link gives an example almost identical to what you're talking about. In the OutputFcn in this example, the "history" structure dynamically accumulates the x and fval data and plots the x's.
  2. Since you say you want a "dynamic graph" I assume you want it to occur while the optimization is in progress. Therefore, the OutputFcn, and not the callback (and also not in your objective function), is the place to do that. The callback will only reacquire control of the program once the optimization has finished.

4 Comments

Thanks for help, apparently I missed the "history" structure way in matlab help. My first question is totally solved now. But, I still got problems on the second one. I can plot a figure in the OutputFcn, but when I tried to embed it into my GUI plot, I failed. What I did is: Before entering outputfcn
global handles % calling handles from GUI callback
In the outputfcn
axes(handles.theta, iter_no) % theta distribution
plot(histry.fval,'o');
hold on
---
What I mean, just calling axes handle as global did not work. What would be an alternative way for it?
Did you also declare "handles" global inside the OutputFcn or in the function it's nested within (I'll call this optmain()) ?
I think it would be better to pass the handles from the GUI workspace directly to optmain as an explicit input argument,e.g.,
function optmain(x0,handles)
history.x=[]
history.fval=[];
fmincon(fun,x0,....)
function OutputFcn(...)
history.x=[history.x, x(:)];
history.fval=[history.fval, fval(:)];
plot(handles.theta,...);
end
end
Once "handles" is in the workspace of optmain() it will also be visible to its nested function OutputFcn()
Thanks for great help again, you really save me some precious time Matt. Explicit input way works proper. For the record, I have declared "global handles" inside the optmain because matlab didn't allow me to put it into innermost nested function, and that did not help me. But, now everything is ok.
OK, I don't know why the global variable approach wouldn't have worked, but it's always better not to use them.

Sign in to comment.

More Answers (0)

Asked:

on 1 Nov 2012

Community Treasure Hunt

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

Start Hunting!