# How to create a hold for the generated plots using optimset, PlotFcns

27 views (last 30 days)
Julio Martinez on 18 Aug 2015
Commented: Julio Martinez on 31 Aug 2015
I am optimizing two objective functions using the 'optimset' function. I have also set the Plot Functions for @optimplotx @optimplotfunccount @optimplotfval @optimplotconstrviolation. How can I 'hold' the two "figures" generated by these two routines? Here's what I typed on my editor:
objFcn = @(x) -CabTRvar(x,TE1, T1a, T1b, T2a, T2b);
%%Parameters to be used by the optimization tool.
x0 = TR;
lb = lowb;
ub = uppb;
%%This section contains the optimization algorithm for absolute contrast
disp('This table shows the optimization table for Absolute Contrast')
options = optimset('Algorithm', 'interior-point', 'Disp','iter');
% The next line generates plots that can be useful to visualize how the
% algorithm is working during an optimization.
options = optimset(options,'PlotFcns', { @optimplotx @optimplotfunccount @optimplotfval @optimplotconstrviolation });
[xOpt, fVal] = fmincon(objFcn, x0,[],[],[],[], lb, ub, [],options);
%%This section contains the optimization algorithm for Normalized contrast
% The function for Normalized contrast (variable TR) is called by this program.
objFcn = @(x) -CabTRvarNorm(x,TE1, T1a, T1b, T2a, T2b);
disp('This table shows the optimization table for Normalized Contrast')
options2 = optimset('Algorithm', 'interior-point', 'Disp','iter');
% The next line generates plots that can be useful to visualize how the
% algorithm is working during an optimization.
options2 = optimset(options2,'PlotFcns', { @optimplotx @optimplotfunccount @optimplotfval @optimplotconstrviolation });
[xOpt2, fVal2] = fmincon(objFcn, x0,[],[],[],[], lb, ub, [],options2);
%%Output: Optimized TR value and its corresponding maximized absolute contrast
fprintf('Optimized TR: \t%4.0f ms\n',xOpt(1));
fprintf('Maximized Absolute Contrast:\t%4.8f\n\n',-fVal);
fprintf('Optimized TR: \t%4.0f ms\n',xOpt2(1));
fprintf('Maximized Normalized Contrast:\t%4.8f\n\n',-fVal2);
When I run this routine, a PlotFcns window shows the first optimization. Then, it is replaced by the second optimization. This is leaving me with only the second "figure". I tried a hold on but it did not work. Do I need to create a hold for these plots? h = plot(something)? I would like to keep both PlotFcns windows -if it's possible- to include in my report. Thanks a lot!

James Wiken on 20 Aug 2015
You can make a copy of the current figure with the following code snippet:
h1 = gcf;
h2 = figure(2); %change this variable for each figure
copyobj(get(h1,'children'),h2);
You can add this code snippet after each call to 'fmincon' to create a copy of the current figure. This will create separate plots for each optimization. If you want to set the plots on the same axes you can follow the directions provided in the answer to the question linked below:

#### 1 Comment

Julio Martinez on 31 Aug 2015
Thank you very much James. This code snippet works like a charm!