MatrixBerryCore
parseparams.m
Go to the documentation of this file.
1 function [reg, prop]=parseparams(args,propNameList,nRegExpected,nPropExpected)
2 import mxberry.core.throwerror;
3 import mxberry.core.ismembercellstr;
4 isUsePropNamesUsed=false;
5 if nargin>=2
6  if ~(isempty(propNameList)&&isnumeric(propNameList))
7  if ischar(propNameList)
8  propNameList={propNameList};
9  end
10  if ~iscellstr(propNameList)
11  throwerror('wrongInput',...
12  'propNameList is expected to be a cell array of strings');
13  end
14  propNameList=cellfun(@lower,propNameList,'UniformOutput',false);
15  isUsePropNamesUsed=true;
16  end
17 end
18 %
19 if ~isUsePropNamesUsed
20  isPropCandidateVec=false(size(args));
21  isPropCandidateVec(length(args)-1:-2:1)=true;
22 else
23  isPropCandidateVec=true(size(args));
24  if numel(isPropCandidateVec)>0
25  isPropCandidateVec(end)=false;
26  end
27 end
28 %
29 isPropCandidateVec=isPropCandidateVec&cellfun('isclass',args,'char');
30 if any(isPropCandidateVec)
31  isPropCandidateVec=isPropCandidateVec&cellfun('ndims',args)==2&...
32  cellfun('size',args,1)==1;
33 end
34 if isUsePropNamesUsed
35  isPropSubVec=ismembercellstr(...
36  cellfun(@lower,args(isPropCandidateVec),'UniformOutput',false)...
37  ,propNameList);
38  isPropCandidateVec(isPropCandidateVec)=isPropSubVec;
39  if ~isempty(isPropCandidateVec)&&...
40  any(isPropCandidateVec&isPropCandidateVec([end,1:end-1]))
41  throwerror('wrongParamList','property list is badly formed');
42  end
43 end
44 indPropVec=find(isPropCandidateVec);
45 if isempty(indPropVec)
46  reg=args;
47  prop={};
48 else
49  prop=args(sort([indPropVec,indPropVec+1]));
50  propResNameList=args(isPropCandidateVec);
51  [isUnique,propUResNameList]=mxberry.core.isunique(propResNameList);
52  if ~isUnique
53  nResProps=length(propResNameList);
54  [~,indThereVec]=ismember(propResNameList,propUResNameList);
55  nResPropSpecVec=accumarray(indThereVec.',ones(nResProps,1));
56  isPropDupVec=nResPropSpecVec>1;
57  dupPropListStr=mxberry.core.cell.cellstr2expression(...
58  propUResNameList(isPropDupVec));
59  throwerror('wrongInput:duplicatePropertiesSpec',...
60  sprintf('properties %s are specified more than once',...
61  dupPropListStr));
62  end
63  isPropCandidateVec(indPropVec+1)=true;
64  reg=args(~isPropCandidateVec);
65 end
66 if nargin>=3
67  if isnumeric(nRegExpected)
68  if isempty(nRegExpected)
69  %do nothing
70  elseif numel(nRegExpected)==1&&...
71  nRegExpected>=0&&fix(nRegExpected)==nRegExpected
72  if numel(reg)~=nRegExpected
73  throwerror('wrongParamList',...
74  ['a number of extracted regular parameters (%d) does ',...
75  'not much an expected number(%d)'],numel(reg),...
76  nRegExpected);
77  end
78  elseif numel(nRegExpected)==2&&nRegExpected(1)>=0&&...
79  nRegExpected(2)>=nRegExpected(1)&&...
80  all(fix(nRegExpected)==nRegExpected)
81  nReg=numel(reg);
82  if ~(nReg<=nRegExpected(2)&&nReg>=nRegExpected(1))
83  throwerror('wrongParamList',...
84  ['a number of extracted regular parameters is ',...
85  'expected to be within range %s'],...
86  mat2str(nRegExpected));
87  end
88  else
89  throwerror('wrongInput',...
90  ['nRegExpected is expected to either a positive integer ',...
91  'scalar or two-element numeric monotinic integer array ']);
92  end
93  else
94  throwerror('wrongInput',...
95  'nRegExpected is expected to be of a numeric type');
96  end
97 end
98 if nargin==4
99  if isempty(nPropExpected)&&isnumeric(nPropExpected)
100  %do nothing
101  elseif numel(nPropExpected)==1&&isnumeric(nPropExpected)&&...
102  nPropExpected>=0&&nPropExpected==fix(nPropExpected)
103  if numel(prop)~=(nPropExpected+nPropExpected)
104  throwerror('wrongParamList',...
105  ['a number of extracted properties (%d) does not much ',...
106  'an expected number of properties (%d)'],...
107  numel(prop)*0.5,nPropExpected);
108  end
109  else
110  throwerror('wrongInput',...
111  ['nPropExpected is expected to be a numeric non-negative ',...
112  'integer scalar']);
113  end
114 end
115 end
116 
function throwerror(in msgTag, in varargin)
THROWERROR works similarly to built-in ERROR function in case when there is no output arguments but s...
function ismembercellstr(in leftList, in rightList, in isHigherIndexUsed)
ISMEMBERCELLSTR produces the same results as the built-in function "ismember" looking for the higher ...
function ismember(in leftVec, in rightVec, in varargin)
ISMEMBER - ismember implementation for arrays of any type.
function parseparams(in args, in propNameList, in nRegExpected, in nPropExpected)
PARSEPARAMS behaves exactly as a built-in Matlab function apart from the incorrect behavior of Matlab...