add optimization plots from fmincon to custom gui axes

1 view (last 30 days)
i would like to add the plots optimplotx and optimplotfval to two axes in my own GUI and without the optimization-plot window popping. can i do this somehow without writing custom plotting-functions? i tried to adjust the axes-objects in the built in plotting functions, but i'm actually new to GUI programming and don't know how to treat the axes handels thoroughly.
thanks for your help!
function stop = optimplotx(x,optimValues,state,varargin)
% OPTIMPLOTX Plot current point at each iteration.
%
% STOP = OPTIMPLOTX(X,OPTIMVALUES,STATE) plots the current point, X, as a
% bar plot of its elements at the current iteration.
%
% Example:
% Create an options structure that will use OPTIMPLOTX
% as the plot function
% options = optimset('PlotFcns',@optimplotx);
%
% Pass the options into an optimization problem to view the plot
% fminbnd(@sin,3,10,options)
% Copyright 2006-2010 The MathWorks, Inc.
% $Revision: 1.1.6.3 $ $Date: 2010/05/10 17:23:45 $
global axes1; %axes of my gui
stop = false;
switch state
case 'iter'
% Reshape if x is a matrix
x = x(:);
xLength = length(x);
xlabelText = sprintf('Number of variables: %g',xLength);
% Display up to the first 100 values
if length(x) > 100
x = x(1:100);
xlabelText = {xlabelText,sprintf('Showing only the first 100 variables')};
end
if optimValues.iteration == 0
% The 'iter' case is called during the zeroth iteration,
% but it now has values that were empty during the 'init' case
plotx = bar(axes1,x);
title(sprintf('Current Point'),'interp','none');
ylabel(sprintf('Current point'),'interp','none');
xlabel(xlabelText,'interp','none');
set(plotx,'edgecolor','none')
set(axes1,'xlim',[0,1 + xLength])
set(plotx,'Tag','optimplotx');
else
plotx = findobj(get(axes1,'Children'),'Tag','optimplotx');
set(plotx,'Ydata',x);
end
end

Answers (1)

Seth DeLand
Seth DeLand on 16 Jan 2012
I'm guessing that when specifying the options structure, you are including optimplotx as a PlotFcn such as:
options = optimset('PlotFcn',@optimplotx)
You can think of Plot functions as a special type of Output function, with some additional code that makes the optimization plot figure. To get what you want you need to do 2 things:
1) Specify the optimplotx function as an Output function:
options = optimset('OutputFcn',@optimplotx)
2) Modify your version of optimplotx to set the axes to the axes that you would like to draw on. It looks like you already have the handle as a global variable "axes1", so after that you can set the axes using:
axes(axes1);
One more thing: Since optimplotx is a function that is shipped with MATLAB, I recommend saving a copy in the directory that you are working in and modifying that version.
  1 Comment
Steven
Steven on 30 Jan 2012
Hi
I have a question that is pretty close to this question, so I hoped you might be able to help me as well…..
I want to modify the the psplotbestf.m custom plot function, but I can’t seem to get it to work …
As soon as change ANY figure or axes properties other than ‘tag’, the psplotbestf.m stops working.
stop = false;
switch flag
case 'init'
h_OptRsltFig = gcf;
set(h_OptRsltFig,'Tag','OptRsltFig');
h_OptRsltAx = gca;
% set(h_OptRsltAx,'Position',[.1 .1 .6 .8]);
set(h_OptRsltAx,'Tag','OptRsltAx');
h_OptRsltPlt = plot(optimvalues.iteration,optimvalues.fval, '.b');
set(h_OptRsltPlt,'Tag','OptRsltPlt');
xlabel('Iteration','interp','none');
ylabel('Function value','interp','none')
case 'iter'
h_OptRsltFig = findobj('Tag','OptRsltFig');
h_OptRsltAx = findobj(get(h_OptRsltFig,'Children'),'Tag','OptRsltAx');
h_OptRsltPlt = findobj(get(h_OptRsltAx,'Children'),'Tag','OptRsltPlt');
newX = [get(h_OptRsltPlt,'Xdata') optimvalues.iteration];
newY = [get(h_OptRsltPlt,'Ydata') optimvalues.fval];
set(h_OptRsltPlt,'Xdata',newX, 'Ydata',newY);
% set(get(h_OptRsltFig,'Title'),'String',['Best Function Value: ',num2str(optimvalues.fval)]);
end
this codes works fine, BUT if I uncomment the set position line, the code doesn’t work. In the case ‘iter’ section, the findobj for the Axes and Plot with the tag fails to find anything, and returns a zero.
So, it seems that setting the “position” property somehow erases the tag property, or otherwise makes the findobj command not work.
Just to see what was going on, I tried changing ‘name’, ‘title’, and a few other properties as well. If I try to set anything other than ‘tag’, the code fails. It puts up the first point (i.e. the init case runs), but doesn’t add any later points, (i.e. the iter case fails to run properly).
Thanks for any help you can offer.
Steve Clarke

Sign in to comment.

Categories

Find more on Optimization 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!