[opt, isdefault]= set_defaults(opt, defopt) [opt, isdefault]= set_defaults(opt, field/value list) This functions fills in the given struct opt some new fields with default values, but only when these fields DO NOT exist before in opt. Existing fields are kept with their original values. There are two forms in which you can can specify the default values, (1) as struct, opt= set_defaults(opt, struct('color','g', 'linewidth',3)); (2) as property/value list, e.g., opt= set_defaults(opt, 'color','g', 'linewidth',3); The second output argument isdefault is a struct with the same fields as the returned opt, where each field has a boolean value indicating whether or not the default value was inserted in opt for that field. The default values should be given for ALL VALID property names, i.e. the set of fields in 'opt' should be a subset of 'defopt' or the field/value list. A warning will be issued for all fields in 'opt' that are not present in 'defopt', thus possibly avoiding a silent setting of options that are not understood by the receiving functions. $Id$ Copyright (C) Fraunhofer FIRST Authors: Frank Meinecke (meinecke@first.fhg.de) Benjamin Blankertz (blanker@first.fhg.de) Pavel Laskov (laskov@first.fhg.de)
0001 function [opt, isdefault]= set_defaults(opt, varargin) 0002 %[opt, isdefault]= set_defaults(opt, defopt) 0003 %[opt, isdefault]= set_defaults(opt, field/value list) 0004 % 0005 % This functions fills in the given struct opt some new fields with 0006 % default values, but only when these fields DO NOT exist before in opt. 0007 % Existing fields are kept with their original values. 0008 % There are two forms in which you can can specify the default values, 0009 % (1) as struct, 0010 % opt= set_defaults(opt, struct('color','g', 'linewidth',3)); 0011 % 0012 % (2) as property/value list, e.g., 0013 % opt= set_defaults(opt, 'color','g', 'linewidth',3); 0014 % 0015 % The second output argument isdefault is a struct with the same fields 0016 % as the returned opt, where each field has a boolean value indicating 0017 % whether or not the default value was inserted in opt for that field. 0018 % 0019 % The default values should be given for ALL VALID property names, i.e. the 0020 % set of fields in 'opt' should be a subset of 'defopt' or the field/value 0021 % list. A warning will be issued for all fields in 'opt' that are not present 0022 % in 'defopt', thus possibly avoiding a silent setting of options that are 0023 % not understood by the receiving functions. 0024 % 0025 % $Id$ 0026 % 0027 % Copyright (C) Fraunhofer FIRST 0028 % Authors: Frank Meinecke (meinecke@first.fhg.de) 0029 % Benjamin Blankertz (blanker@first.fhg.de) 0030 % Pavel Laskov (laskov@first.fhg.de) 0031 0032 if length(opt)>1, 0033 error('first argument must be a 1x1 struct'); 0034 end 0035 0036 % Set 'isdefault' to ones for the field already present in 'opt' 0037 isdefault= []; 0038 if ~isempty(opt), 0039 for Fld=fieldnames(opt)', 0040 isdefault= setfield(isdefault, Fld{1}, 0); 0041 end 0042 end 0043 0044 % Check if we have a field/value list 0045 if length(varargin) > 1 0046 0047 % If the target is a propertylist structure use propertylist2struct to 0048 % convert the property list to a defopt structure. 0049 if (ispropertystruct(opt)) 0050 defopt = propertylist2struct(varargin{:}); 0051 0052 else % otherwise construct defopt from scratch 0053 0054 0055 % Create a dummy defopt structure: a terrible Matlab hack to overcome 0056 % impossibility of incremental update of an empty structure. 0057 defopt = struct('matlabsucks','foo'); 0058 0059 % Check consistency of a field/value list: even number of arguments 0060 nArgs= length(varargin)/2; 0061 if nArgs~=round(nArgs) & length(varargin~=1), 0062 error('inconsistent field/value list'); 0063 end 0064 0065 % Write a temporary defopt structure 0066 for ii= 1:nArgs, 0067 defopt= setfield(defopt, varargin{ii*2-1}, varargin{ii*2}); 0068 end 0069 0070 % Remove the dummy field from defopt 0071 defopt = rmfield(defopt,'matlabsucks'); 0072 end 0073 0074 else 0075 0076 % If varargin has only one element, it must be a defopt structure. 0077 defopt = varargin{1}; 0078 0079 end 0080 0081 % Replace the missing fields in 'opt' from their 'defopt' counterparts. 0082 for Fld=fieldnames(defopt)', 0083 fld= Fld{1}; 0084 if ~isfield(opt, fld), 0085 opt= setfield(opt, fld, getfield(defopt, fld)); 0086 isdefault= setfield(isdefault, fld, 1); 0087 end 0088 end 0089 0090 % Check if some fields in 'opt' are missing in 'defopt': possibly wrong 0091 % options. 0092 for Fld=fieldnames(opt)', 0093 fld= Fld{1}; 0094 if ~isfield(defopt,fld) 0095 % warning('set_defaults:DEFAULT_FLD',['field ''' fld ''' does not have a valid default option']); 0096 end 0097 end 0098 0099 0100 function t = ispropertystruct(opts) 0101 % ISPROPERTYSTRUCT - Check whether a structure contains optional parameters 0102 % 0103 % T = ISPROPERTYSTRUCT(OPTS) 0104 % returns 1 if OPTS is a structure generated by PROPERTYLIST2STRUCT. 0105 % 0106 % 0107 % See also PROPERTYLIST2STRUCT 0108 % 0109 0110 % Copyright Fraunhofer FIRST.IDA (2004) 0111 % $Id: ispropertystruct.m,v 1.1 2004/08/16 11:52:17 neuro_toolbox Exp $ 0112 0113 error(nargchk(1, 1, nargin)); 0114 % Currently, we do not check the version number. Existence of the field 0115 % is enough to identify the opts structure as a property list 0116 t = isfield(opts, 'isPropertyStruct'); 0117 0118