Optimization plot in App Designer
9 views (last 30 days)
Show older comments
My App Designer program runs an optimization process. Specifically with the fmincon algorithm.
I would like to plot the real-time optimization process so that the user can watch it evolve. Specifically I want to plot 'optimplotx' and 'optimplotfval'.
However, I would like to plot these two graphs within two UIAxes elements in my App, and not in a pop-up window as it does by default.
My question is if it is possible to easily transfer those two plots that the optimization algorithm create (and updates) by default to their respective UIAxes elements.
By easily I mean not having to code all the plotting process (input data, plotting, updating...) but rather "associating" the actual graphs that pop-up to the UIAxes in the App.
Thanks in advance.
0 Comments
Answers (1)
Kevin Holly
on 2 Aug 2022
You could get the handle of the axes and transfer the children to the uiaxes in your app. See example code below:
fun = @(x)100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
nonlcon = @unitdisk;
A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = [];
x0 = [0,0];
options = optimoptions('fmincon','PlotFcn',@optimplotx);
[x,fval,exitflag,output,lambda,grad,hessian] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
h = gca;
h.Children.Parent = app.UIAxes;
app.UIAxes.XLabel.String = h.XLabel.String;
app.UIAxes.YLabel.String = h.YLabel.String;
app.UIAxes.Title.String = h.Title.String;
app.UIAxes.XLim = h.XLim;
app.UIAxes.YLim = h.YLim;
close(gcf)
2 Comments
Kevin Holly
on 2 Aug 2022
Here is an example if you are transferring two plots:
fun = @(x)100*(x(2)-x(1)^2)^2 + (1-x(1))^2;
nonlcon = @unitdisk;
A = [];
b = [];
Aeq = [];
beq = [];
lb = [];
ub = [];
x0 = [0,0];
options = optimoptions('fmincon','PlotFcn',{@optimplotx,@optimplotfval});
[x,fval,exitflag,output,lambda,grad,hessian] = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
h=gcf;
app.UIAxes.XLim = h.Children(end).XLim;
app.UIAxes.YLim = h.Children(end).YLim;
h.Children(end).Children.Parent = app.UIAxes;
app.UIAxes.XLabel.String = h.Children(end).XLabel.String;
app.UIAxes.YLabel.String = h.Children(end).YLabel.String;
app.UIAxes.Title.String = h.Children(end).Title.String;
app.UIAxes2.XLim = h.Children(end-1).XLim;
app.UIAxes2.YLim = h.Children(end-1).YLim;
h.Children(end-1).Children.Parent = app.UIAxes2;
app.UIAxes2.XLabel.String = h.Children(end-1).XLabel.String;
app.UIAxes2.YLabel.String = h.Children(end-1).YLabel.String;
app.UIAxes2.Title.String = h.Children(end-1).Title.String;
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!