Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Combining/Overwriting structs

1 view
Skip to first unread message

Ralph

unread,
Mar 11, 2010, 2:09:06 PM3/11/10
to
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?

Thanks,
Ralph

jason

unread,
Mar 11, 2010, 2:17:41 PM3/11/10
to

why not initialize options.color to defaults.color
then set
finaloptions.color=options.color
?

Matt J

unread,
Mar 11, 2010, 2:22:06 PM3/11/10
to

Ralph

unread,
Mar 11, 2010, 2:27:02 PM3/11/10
to
jason <jason....@gmail.com> wrote in message <1573bdf5-

> why not initialize options.color to defaults.color
> then set
> finaloptions.color=options.color
> ?

Because the only thing the user should need to do is to setup those option they want to change.

Yes, I could give them the defaults-structure but I don't want to. And yes, it's only cosmetics, I know.

Ralph

Loren Shure

unread,
Mar 11, 2010, 2:27:44 PM3/11/10
to

Matt J

unread,
Mar 11, 2010, 2:33:21 PM3/11/10
to
"Matt J " <mattja...@THISieee.spam> wrote in message <hnbfsu$nq8$1...@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{:});

Bjorn Gustavsson

unread,
Mar 11, 2010, 3:48:24 PM3/11/10
to
"Ralph " <news...@ecuapac.dyndns.org> wrote in message <hnbf4i$4ao$1...@fred.mathworks.com>...
Yup, there is catstruct.m to be found on the file exchange:
http://www.mathworks.com/matlabcentral/fileexchange/7842-catstruct

HTH.
Bjoern

Matt J

unread,
Mar 11, 2010, 3:59:06 PM3/11/10
to
"Bjorn Gustavsson" <bj...@irf.se> wrote in message <hnbkuo$o5a$1...@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...

0 new messages