-
Notifications
You must be signed in to change notification settings - Fork 5
/
structfind.m
119 lines (118 loc) · 2.83 KB
/
structfind.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
function index=structfind(a,field,value)
% StructFind, Find the index of a certain string or value in a struct
%
% index=structfind(a,field,value)
%
% inputs,
% a : A Matlab struct, for example a(1).name='red', a(2).name='blue';
% field : The name of the field which is searched, for example 'name'
% value : The search value, for example 'blue'
%
% outputs,
% index : The Struct index which match the search
%
%
% Example,
%
% a(1).name='blue';
% a(1).index=0;
% a(1).val='g';
%
% a(2).name='red';
% a(2).index=1;
% a(2).val=[1 0];
%
% a(3).name='green';
% a(3).index=2;
% a(3).val='g';
%
% a(4).name='black';
% a(4).index=3;
% a(4).val=[0 0 0];
%
% a(5).name='yellow';
% a(5).index=NaN;
% a(5).val=[0 1 1];
%
% a(6).name='orange';
% a(6).index=[];
% a(6).val=[1 1 0];
%
% a(7).name='brown';
% a(7).index=6;
% a(7).val={'12'};
%
% a(8).name='white';
% a(8).index=7;
% a(8).val.x=1;
%
% a(8).name='purple';
% a(8).index=8;
% a(8).val.child.value=2;
%
% index=structfind(a,'name','red');
% disp(['index : ' num2str(index)])
%
% index=structfind(a,'index',1);
% disp(['index : ' num2str(index)])
%
% index=structfind(a,'val',[0 0 0]);
% disp(['index : ' num2str(index)])
%
% index=structfind(a,'val','g');
% disp(['index : ' num2str(index)])
%
% index=structfind(a,'index',NaN);
% disp(['index : ' num2str(index)])
%
% index=structfind(a,'index',[]);
% disp(['index : ' num2str(index)])
%
% index=structfind(a,'val',{'12'});
% disp(['index : ' num2str(index)])
%
% index=structfind(a,'val.child.value',2);
% disp(['index : ' num2str(index)])
%
% Function is written by D.Kroon University of Twente (December 2010)
% We don't compare structs
if(isstruct(value)),
error('structfind:inputs','search value can not be a struct');
end
% Stop if field doesn't exist
if(~isfield(a,field))
index=find(arrayfun(@(x)(cmp(x,field,value)),a,'uniformoutput',true));
else
index=find(arrayfun(@(x)(cmp(x,field,value)),a,'uniformoutput',true));
end
function check=cmp(x,field,value)
check=false;
if(isfield(x,field))
% Simple field like x.tag
x=x.(field);
else
% Complex field like x.tag.child.value
in=find(field=='.');
s=[1 in+1]; e=[in-1 length(field)];
for i=1:length(s)
fieldt=field(s(i):e(i));
if(isfield(x,fieldt)), x=x.(fieldt); else return; end
end
end
% We don't compare structs
if(isstruct(x)), return; end
% Values can only be equal, if they equal in length
if(length(x)==length(value)),
% This part compares the NaN values
if((~iscell(x))&&(~iscell(value))&&any(isnan(value))),
checkv=isnan(value); checkx=isnan(x);
if(~all(checkx==checkv)), return; end
x(checkx)=0; value(checkv)=0;
end
% This part compares for both string as numerical values
if(iscell(x)||iscell(value))
check=all(strcmp(x,value));
else
check=all(x==value);
end
end