Mercurial > hg > ltpda
diff m-toolbox/classes/@plist/subset.m @ 0:f0afece42f48
Import.
author | Daniele Nicolodi <nicolodi@science.unitn.it> |
---|---|
date | Wed, 23 Nov 2011 19:22:13 +0100 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/m-toolbox/classes/@plist/subset.m Wed Nov 23 19:22:13 2011 +0100 @@ -0,0 +1,134 @@ +% SUBSET returns a subset of a parameter list. +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% DESCRIPTION: SUBSET returns a subset of a parameter list. +% +% CALL: p = subset(pl, 'key') +% p = subset(pl, search_pl) +% p = subset(pl, 'key1', 'key2') +% p = subset(pl, {'key1', 'key2'}) +% +% A warning is given for any key not in the original plist. +% +% <a href="matlab:utils.helper.displayMethodInfo('plist', 'subset')">Parameters Description</a> +% +% VERSION: $Id: subset.m,v 1.6 2011/04/08 08:56:20 hewitson Exp $ +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +function varargout = subset(varargin) + + %%% Check if this is a call for parameters + if utils.helper.isinfocall(varargin{:}) + varargout{1} = getInfo(varargin{3}); + return + end + + % Check inputs + if nargin < 2 + error('### Incorrect inputs') + end + + if nargout ~= 1 + error('### Incorrect outputs. plist/subset cannot be used as a modifier.'); + end + + % The first input is the plist to extract the subset of + pl = varargin{1}; + + % Collect the keys to search for + keys = {}; + for kk=2:nargin + if iscell(varargin{kk}) + keys = [keys varargin{kk}]; + elseif isa(varargin{kk}, 'plist') + pli = varargin{kk}; + ikeys = pli.find('keys'); + if ~isempty(ikeys) + keys = [keys ikeys]; + end + else + keys = [keys varargin(kk)]; + end + end + + % Convert input keys to upper case + keys = upper(keys); + + % Check we got some keys + if isempty(keys) + error('### Please specify at least one key'); + end + + % We only handle one input plist + if numel(pl) ~= 1 + error('### This function can only work with one plist-object'); + end + + % Get parameters we want + pl_out = plist(); + for kk=1:pl.nparams + if ismember(pl.params(kk).key, keys) + pl_out.append(copy(pl.params(kk),1)); + end + end + + % Check the plist contains all the requested keys + for kk=1:numel(keys) + if pl_out.nparams == 0 || ~ismember(keys{kk}, {pl_out.params(:).key}) + warning('The key ''%s'' was not found in the original plist', keys{kk}); + end + end + + % Set output + if nargout == 1 + varargout{1} = pl_out; + else + error('### Incorrect number of outputs'); + end +end + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% Local Functions % +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% FUNCTION: getInfo +% +% DESCRIPTION: Get Info Object +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +function ii = getInfo(varargin) + if nargin == 1 && strcmpi(varargin{1}, 'None') + sets = {}; + pl = []; + else + sets = {'Default'}; + pl = getDefaultPlist; + end + % Build info object + ii = minfo(mfilename, 'plist', 'ltpda', utils.const.categories.helper, '$Id: subset.m,v 1.6 2011/04/08 08:56:20 hewitson Exp $', sets, pl); + ii.setModifier(false); + ii.setArgsmin(1); +end + +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% +% +% FUNCTION: getDefaultPlist +% +% DESCRIPTION: Get Default Plist +% +%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% + +function pl = getDefaultPlist() + + pl = plist(); + + % Keys + p = param({'keys', 'The keys to search for.'}, paramValue.EMPTY_STRING); + pl.append(p); + +end +