From: Matt J on
"Matt J " <mattjacREMOVE(a)THISieee.spam> wrote in message <hnbfsu$nq8$1(a)fred.mathworks.com>...
>
>
> http://www.eecs.umich.edu/~fessler/irt/irt/utilities/vararg_pair.m

Incidentally, the above file requires that you pass the options as string-value pairs. Below is a wrapper that I routinely use, which will allow you to pass options in structure form as well.


function [varargout]=optionproc(default, options, varargin)
%Process options encapsulated as either a structure or as a cell array
%
%[optionstruct,extras]=optionproc(default, options, varargin)
%
% default: structure of default values
% options: a structure whose fields are given values or a cell array
% of the form {'Option1',Option1,'Option2',Option2,...}
%
% varargin: arguments to be passed to vararg_pair.m


nn=length(options);

if nn==0

optionstruct=default;
varargout{1}=default;
varargout{2}={};
return

elseif iseven(nn)

varargs=options;

elseif nn==1 %structure input mode

if iscell(options),
options=options{1};
end

if ~isstruct(options), error 'Attempted structure mode input?'; end

varargs=[fieldnames(options),struct2cell(options)]';
varargs=varargs(:)';

else
error 'Unrecognized Input'
end


varargout=cell(1,nargout);
[varargout{:}]=vararg_pair(default,varargs,varargin{:});
From: Bjorn Gustavsson on
"Ralph " <news2008(a)ecuapac.dyndns.org> wrote in message <hnbf4i$4ao$1(a)fred.mathworks.com>...
> Hallo!
>
> I'm setting up some default options with a struct:
> defaults.color = 'r';
> defaults.marker = 'o';
> defaults.text = false;
>
> A user can now change some of the default option. e.g.:
> options.color = 'k';
>
> Now I'd like to overwrite the default options with the options changed by the user:
> finaloptions = Overwrite(defaults, options)
>
> Giving:
> finaloptions.color = 'k';
> finaloptions.marker = 'o';
> finaloptions.text = false;
>
> Is there a function that does this for me or do I have to program it myself?
>
Yup, there is catstruct.m to be found on the file exchange:
http://www.mathworks.com/matlabcentral/fileexchange/7842-catstruct

HTH.
Bjoern
From: Matt J on
"Bjorn Gustavsson" <bjonr(a)irf.se> wrote in message <hnbkuo$o5a$1(a)fred.mathworks.com>...

> Yup, there is catstruct.m to be found on the file exchange:
> http://www.mathworks.com/matlabcentral/fileexchange/7842-catstruct
==============

Careful.

Since catstruct() will always consolidate the fields, it means that
it will be susceptible to typos and spelling mistakes.

For example, if default.color='k', but someone inputs an option accidentally using a British spelling like options.colour='g', then default.color will not be overwritten.

These kinds of errors can be hard to detect...
First  |  Prev  | 
Pages: 1 2
Prev: reduce matrix size
Next: simultaneous equations system