MatrixBerryCore
pathfilterstruct.m
Go to the documentation of this file.
1 function ResStruct=pathfilterstruct(InpStruct,path2KeepList)
2 if isempty(path2KeepList)
3  ResStruct=InpStruct;
4 else
5  %
6  if ischar(path2KeepList)
7  path2KeepList={path2KeepList};
8  end
9  %make sure that all the restrictions are applied i.e. if {'a','a.b'} is
10  %specified as path2KeepList 'a.b' is used
11  %
12  path2KeepList=sort(path2KeepList);
13 
14  nPath=length(path2KeepList);
15  pathCell=cell(nPath,1);
16  for iPath=1:nPath
17  pathCell{iPath}=regexp(path2KeepList{iPath},'([^\.]*)','match');
18  end
19  %
20  lengthVec=cellfun(@length,pathCell);
21  maxPathLength=max(lengthVec);
22  fieldSeqCell=cell(nPath,maxPathLength);
23  %
24  for iPath=1:nPath
25  fieldSeqCell(iPath,1:lengthVec(iPath))=pathCell{iPath};
26  end
27 
28  [field2KeepList,~,indRep]=unique(fieldSeqCell(:,1));
29  %
30  ResStruct=mxberry.core.struct.fieldfilterstruct(InpStruct,field2KeepList);
31  %
32 
33  indCell=accumarray(indRep,(1:nPath).',[],@(x){x});
34  nUniqueFields=length(indCell);
35  %
36  for iField=1:nUniqueFields
37  fieldName=field2KeepList{iField};
38  subFieldSeqCell=fieldSeqCell(indCell{iField},:);
39  if size(subFieldSeqCell,2)>1
40  subPath2KeepList=sepcell2pathlist(subFieldSeqCell(:,2:end));
41  InpSubStruct=auxgetfieldstruct(ResStruct,fieldName);
42  ResCell=num2cell(mxberry.core.struct.pathfilterstruct(InpSubStruct,...
43  subPath2KeepList));
44  ResSubStruct=struct(fieldName,ResCell);
45  %
46  ResStruct=mxberry.core.struct.binaryunionstruct(ResStruct,...
47  ResSubStruct,@(x,y)y);
48  end
49  end
50 end
51 end
52 %
53 function SRes=auxgetfieldstruct(SInp,fieldName)
54 SRes=reshape([SInp.(fieldName)],size(SInp));
55 end
56 %
57 function pathList=sepcell2pathlist(sepCell)
58 nPath=size(sepCell,1);
59 pathList={};
60 for iPath=1:nPath
61  isnEmpty=~cellfun(@isempty,sepCell(iPath,:));
62  indLastNotEmpty=find(isnEmpty,1,'last');
63  if isempty(indLastNotEmpty)
64  pathList={};
65  return;
66  end
67  curPathCell=sepCell(iPath,1:indLastNotEmpty);
68  curPathCell=[repmat({'.'},1,indLastNotEmpty);curPathCell]; %#ok<AGROW>
69  curPath=horzcat(curPathCell{:});
70  pathList=[pathList,{curPath}]; %#ok<AGROW>
71 end
72 %
73 end
function pathfilterstruct(in InpStruct, in path2KeepList)
PATHFILTERSTRUCT leaves in input structure array only specified paths.
function unique(in inpVec)
UNIQUE for arrays of any type.