When using Optimoptions, how can I tell if an option was user-set, or a default.

1 view (last 30 days)
I am writing a function which is effectively a wrapper for an optimization function (specifically lsqcurvefit, but it doesn't matter). Outside my wrapper, I want to set some options, like:
optionsIn = optimoptions(@lsqcurvefit, ...[tolerance settings etc.])
where optionsIn is passed to the wrapper as an argument. Inside the wrapper, which will run in a loop, I want to turn off display:
options = optimoptions(optionsIn, 'Display','none').
However, I might want to override that setting when I call the wrapper (e.g. turning display on for debugging). The trouble is that optimoptions doesn't distinguish between the case where Display was left as default (in which case I would want to override it inside the wrapper), and the case where it has been set manually (in which case I do not want to override it).
With the old optimset function, I could do this like:
if isempty(optimget(optionsIn, 'Display'))
options = optimset(optionsIn, 'Display','off');
end
but I can find no equivalent for optimoptions. Is there any way to do this?
Note 1: I know optimoptions does keep track of this information, because it displays like (my arrows):
options =
lsqcurvefit options:
Options used by current Algorithm ('trust-region-reflective'):
(Other available algorithms: 'levenberg-marquardt')
Set by user: <=======================
Display: 'iter'
Default: <=======================
Algorithm: 'trust-region-reflective'
DerivativeCheck: 'off'
... ...
Note 2: Obviously I can work around this by just using optimset. But it seems to be a legacy option these days, and I would like to move away from it before I am forced to.

Answers (1)

Alan Weiss
Alan Weiss on 2 Jun 2017
I think that you can simply check the value of options.Display.
options = optimoptions('lsqcurvefit','Display','none');
options.Display
ans =
'none'
options = optimoptions('lsqcurvefit');
options.Display
ans =
'final'
Alan Weiss
MATLAB mathematical toolbox documentation
  5 Comments
Steven Lord
Steven Lord on 5 Jun 2017
I believe you are correct that we do not expose to users the information about whether an option whose value is equal to the default was set to the default by the user or has the default value because it was not set at all. At least there isn't a documented way to do that.
Asking for a documented way to determine whether or not an option was set by the user does sound to me like a reasonable enhancement request.
David Szwer
David Szwer on 5 Jun 2017
Thank you for confirming that! I hope this information does get exposed in a future version, at least before optimset gets removed completely... I'll look into making a feature request.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!