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

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.

Risposte (1)

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 Commenti

Sorry, that is not quite what I want. Suppose I do something like this:
% Calling script defines:
op = optimoptions(@lsqcurvefit);
% And other options are set.
% op passed to fitting function wrapper, that contains:
if strcmpi(op.Display,'final') % The default option.
op.Display = 'none';
end
This will work unless the user deliberately tries to set the display to 'final' - in that case their setting will be overwritten. I suppose the user could choose 'final-detailed' instead, but they would need to look up what the defaults are so they can avoid them.
To be clear, this is a very minor problem, and I can easily work around it for now. But it just seems silly that the information is there in the options object, and we can't get at it.
Oh, sorry, I didn't read your question carefully.
If you have R2016a or later, you can possibly use the resetoptions function to set particular options back to their defaults. And, of course, you can override particular options by just overwriting them.
opdop = op;
op = resetoptions(op,'FunctionTolerance'); % reset to default
op = optimoptions(op,'Display','final'); % override
% do your calculations here
op = oldop; % return to the starting value
This doesn't do exactly what you want, I think, but maybe it will suffice.
Alan Weiss
MATLAB mathematical toolbox documentation
Sorry, I think maybe my question wasn't clear enough. I have reworded the first paragraph to try to help.
I'm guessing that this just isn't possible with the existing setup. Now that options settings are objects rather than structures, they can hide useful information where we can't get at it.
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.
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.

Accedi per commentare.

Richiesto:

il 1 Giu 2017

Commentato:

il 5 Giu 2017

Community Treasure Hunt

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

Start Hunting!

Translated by