MatrixBerryCore
structgetpath.m
Go to the documentation of this file.
1 function [SRes,isThere]=structgetpath(SInpArr,pathStr,isPresenceChecked,isJustCheck)
2 import mxberry.core.throwerror;
3 import mxberry.core.check.lib.iscellofstring;
4 import mxberry.core.check.lib.isstring;
5 if ~isstruct(SInpArr)
6  throwerror('wrongInpt','SInpArr is expected to be a structure');
7 end
8 %
9 if ~(isstring(pathStr)||ischar(pathStr)&&isempty(pathStr)||...
10  iscellofstring(pathStr))
11  throwerror('wrongInpt',['pathStr is expected to be ',...
12  'either a string or a list of strings']);
13 end
14 if nargin<4
15  isJustCheck=false;
16  if nargin<3
17  isPresenceChecked=false;
18  elseif ~(islogical(isPresenceChecked)&&isscalar(isPresenceChecked))
19  throwerror('wrongInpt',...
20  'isPresenceChecked is expected to be a logical scalar');
21  end
22 elseif ~(islogical(isJustCheck)&&isscalar(isJustCheck))
23  throwerror('wrongInpt',...
24  'isJustCheck is expected to be a logical scalar');
25 end
26 SRes=struct.empty(0,0);
27 %
28 if isJustCheck
29  isPresenceChecked=true;
30 end
31 %
32 if isempty(pathStr)
33  if ~isJustCheck
34  SRes=SInpArr;
35  end
36  isThere=true;
37 else
38  if ischar(pathStr)
39  fieldNameList=regexp(pathStr,'([^\.]*)','match');
40  else
41  fieldNameList=pathStr;
42  end
43  %
44  firstField=fieldNameList{1};
45  %
46  if isscalar(SInpArr)
47  [SResArr,isThere]=getField(SInpArr,firstField,...
48  isPresenceChecked);
49  %
50  if isThere
51  if length(fieldNameList)>1
52  [SRes,isThere]=mxberry.core.struct.structgetpath(SResArr,...
53  fieldNameList(2:end),isPresenceChecked,isJustCheck);
54  else
55  SRes=SResArr;
56  end
57  end
58  else
59  [SResCArr,isThereCArr]=arrayfun(...
60  @(x)getField(x,firstField,isPresenceChecked),...
61  SInpArr,'UniformOutput',false);
62  isThere=all([isThereCArr{:}]);
63  %
64  if isThere
65  if isJustCheck
66  if length(fieldNameList)>1
67  [~,isThereArr]=cellfun(...
68  @(x)mxberry.core.struct.structgetpath(x,...
69  fieldNameList(2:end),isPresenceChecked,isJustCheck),...
70  SResCArr,'UniformOutput',false);
71  isThere=all([isThereArr{:}]);
72  end
73  else
74  try
75  SResArr=cell2mat(SResCArr);
76  catch meObj
77  if isPresenceChecked
78  isThere=false;
79  else
80  rethrow(meObj);
81  end
82  end
83  if isThere
84  if length(fieldNameList)>1
85  [SRes,isThere]=...
86  mxberry.core.struct.structgetpath(SResArr,...
87  fieldNameList(2:end),isPresenceChecked,...
88  isJustCheck);
89  else
90  SRes=SResArr;
91  end
92  end
93  end
94  end
95  end
96 end
97 end
98 function [SRes,isThere]=getField(SInpArr,fieldName,isPresenceChecked)
99 if isPresenceChecked
100  if isfield(SInpArr,fieldName)
101  isThere=true;
102  SRes=SInpArr.(fieldName);
103  else
104  isThere=false;
105  SRes=struct.empty(0,0);
106  end
107 else
108  SRes=SInpArr.(fieldName);
109  isThere=true;
110 end
111 end
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 structgetpath(in SInpArr, in pathStr, in isPresenceChecked, in isJustCheck)
STRUCTGETPATH extract a value from a given structure array using a path specified either as a string ...
function iscellofstring(in inpArray)
function isstring(in inpArray)
function cell2mat(in inpCArr)
CELL2MAT does the same as the built-in cell2mat function but a) 20% faster b) works with cell arrays ...
function getField(in SInpArr, in fieldName, in isPresenceChecked)