MATLAB: How to change the formatting of the table displayed by FMINCON in the Optimization Toolbox 6.2

Optimization Toolbox

I am using FMINCON with the 'Display' parameter set to 'iter'. I would like to change the formatting of the printed table. That is, I would like to use fixed point (0.001) instead of scientific notation (1e-3).

Best Answer

There is no way to directly set the format.
As a workaround, you will need to set 'Display' to 'final' and create a custom 'OutputFcn'. This function can use its input arguments to mimic the table generated by setting 'Display' to 'iter'.
Below is an example that mimics the table produced by FMINCON. Please change the code to meet your needs. If you want to prevent scientific formatting on numbers, you will probably want to change the "g"s to "f"s in the variable 'formatstr'. For example, change "%12.6g" to "%12.6f".
The variable 'formatstr' is coppied from NLCONST, the function called by FMINCON to perform the optimization. To make a similar table that is for another optimization function, you may need to search through the code in the Optimization Toolbox to find where the table is printed in that function.
A = [-1 -2 -2; ...
1 2 2];
b = [0;72];
x0 = [10;10;10]; % Starting guess at the solution
f = @(x)-x(1) * x(2) * x(3);
opt2 = optimset('Display','final','Algorithm','active-set','OutputFcn', @custom_output);
fmincon(f,x0,A,b,[],[],[],[],[],opt2);
function stop = custom_output(~, optimValues, state)
stop = false;
header = [' Max Line search Directional First-order\n' ...
' Iter F-count f(x) constraint steplength derivative optimality Procedure\n'];
formatstr = '%5.0f %5.0f %12.6g %12.4g %12.3g %12.3g %12.3g %s %s';
switch state
case 'init'
fprintf(header);
case 'iter'
iter = optimValues.iteration;
funcCount = optimValues.funccount;
f = optimValues.fval;
con = optimValues.constrviolation;
laststep = optimValues.lssteplength;
grad = optimValues.gradient;
direcderiv = optimValues.directionalderivative;
fo = optimValues.firstorderopt;
hessUpdateMsg = optimValues.procedure;
fprintf(formatstr,iter,funcCount,f,con,laststep,direcderiv,fo,hessUpdateMsg);
fprintf('\n');
end