-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhasfield.m
64 lines (51 loc) · 1.42 KB
/
hasfield.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function [ x,I ] = hasfield( s, fname, varargin)
% X = HASFIELD(S,FNAME) checks if the struct S has the fieldname FNAME. If
% S is not a struct, X will be false.
%
% [X,L] = HASFIELD(S,FNAME,LEVEL) checks LEVEL number of levels into possible
% nested structres for a field name. L returns the first level that the
% field name was found at.
L = 0;% level
I = 0;% level iteratios
x = 0;% default
if(~isstruct(s))
warning('input is not a struct');
return
end
if(length(varargin) > 0)
if(~isnumeric(varargin{1}))
error('LEVEL must be a numeric variable');
end
L = varargin{1};
end
recurseon({s});
function recurseon(T)
I = I+1;
%return if max iterations
if(L > 0 && I > L)
I = I-1;
return
end
%check for any matching fieldnames
NEWT = {};
for i=1:length(T)
t = T{i};
%detection
x = isfield(t,fname);
if(x)
return
end
%breadth first elaboration
N = fieldnames(t);
for j = 1:length(N)
if(isstruct(t.(N{j})))
NEWT = vertcat(NEWT,t.(N{j}));
end
end
end
%if theres actually anything to recurse on
if(~isempty(NEWT))
recurseon(NEWT);
end
end
end