Passing an UIAxes handle to PlotFcn

Hi guys,
I am having hard time wrapping my mind around anonymous functions. I am running an optimisation problem, and I would like to make the 'optimplotfval' function to plot on supplied UIAxes handle.
I know that I can create my own plot function or copy the optimplotfval and rename it, but I am wondering how to pass UIAxes handle to any of those cases.
I would be grateful for ideas.
Thanks.

1 件のコメント

Mario Malic
Mario Malic 2020 年 9 月 21 日
beep boop, any ideas?

サインインしてコメントする。

 採用された回答

Adam Danz
Adam Danz 2020 年 9 月 21 日
編集済み: Adam Danz 2020 年 9 月 21 日

2 投票

tl;dr use the OutputFcn method below.
Using a custom optimplotfval
"I would like to make the 'optimplotfval' function to plot on supplied UIAxes handle"
When PlotFcns is set to optimplotfval, the optimizer searches for an existing figure named 'Optimization Plot Function' and if it doesn't exist, it creates one. The optimization outputs are plotted on that figure and there is no option to pass an alternative axis handle.
However, you can specify your own plotting function that does contain an axis handle input but the 'Optimization Plot Function' figure will be created anyway even though it will be ignored. Therefore, this approach is not the one you want but I'll show it anyway JFF (just for fun).
options = optimset('PlotFcns',@(a,b,c)customOptimplotfval(app.UIAxes,a,b,c),'TolX',1e-7)
% Notice the axis handle passed here ^^^^^^^^^^
function stop = customOptimplotfval(ax,~,optiVals,~)
% Assumes this has already been executed: hold(app.UIAxes, 'on')
plot(ax, optiVals.iteration, optiVals.fval, 'm*')
stop = false;
end
Using OutputFcn to control plotting
To bypass the problems described above, use an OutputFcn to plot the results of each iteration of the optimization.
% Create demo uiaxes
app.UIAxes = uiaxes();
pause(3) % wait for axis to appear before the show's over.
% Important: apply "hold on" prior to running the optimization.
cla(app.UIAxes)
hold(app.UIAxes, 'on')
% Set up and perform optimization
options = optimset('OutputFcn',@(x,optiVals,state)outfun(app.UIAxes,x,optiVals,state),'TolX',1e-7);
% Notice the axis handle passed here ^^^^^^^^^^
fun = @(x)100*((x(2) - x(1)^2)^2) + (1 - x(1))^2; % Rosenbrock's function
x0 = [-1,2];
[x,fval] = fminsearch(fun,x0,options);
% Define OutputFcn
function stop = outfun(ax,~,optiVals,~)
% Assumes this has already been executed: hold(app.UIAxes, 'on')
plot(ax, optiVals.iteration, optiVals.fval, 'm*')
drawnow % optional; also try drawnow limitrate
stop = false;
end

3 件のコメント

Mario Malic
Mario Malic 2020 年 9 月 22 日
編集済み: Mario Malic 2020 年 9 月 29 日
Amazing, works like a charm on OutputFcn. Thank you very much, again, Adam!
When PlotFcn(s) is used, it looks like the figure is created outside of called plot function. It's possible to hide it, but it's better to use OutputFcn
set(findobj('Name', 'Optimization PlotFcns'), 'Visible', 'off') % JFF
Adam Danz
Adam Danz 2020 年 9 月 22 日
Yes, the fig is generated in fminsearch > callOutputAndPlotFcn.
callOutputAndPlotFcn then calls callAllOptimPlotFcns after the figure is generated and that function uses feval to apply the plotting function.
Mario Malic
Mario Malic 2020 年 9 月 22 日
Great to know, thanks.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeGraphics Performance についてさらに検索

製品

リリース

R2020a

質問済み:

2020 年 9 月 20 日

編集済み:

2020 年 9 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by