-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchfield.m
171 lines (157 loc) · 4.72 KB
/
searchfield.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
function searchfield(str,txt,show)
%function searchfield(str,txt,show)
% search within a structure
%
% str = structure/object
% txt = text to find (char)
% show = show value (true/false)
if ~isstruct(str) && ~isobject(str); error('str must be a struct/object'); end
if ~isa(txt,'char'); error('txt must be char'); end
if ~exist('show'); show=false; end
if ~isequal(show,1) && ~isequal(show,0); error('show must be 0 or 1'); end
global TXT;
TXT = txt;
unfold(str,inputname(1),show==1)
%%
%
% https://www.mathworks.com/matlabcentral/answers/94122-is-there-a-function-that-will-display-all-the-fields-of-a-nested-structure-in-matlab
function unfold(SC,varargin)
%UNFOLD Unfolds a structure.
% UNFOLD(SC) displays the content of a variable. If SC is a structure it
% recursively shows the name of SC and the fieldnames of SC and their
% contents. If SC is a cell arraythe contents of each cell are displayed.
% It uses the caller's workspace variable name as the name of SC.
% UNFOLD(SC,NAME) uses NAME as the name of SC.
% UNFOLD(SC,SHOW) If SHOW is false only the fieldnames and their sizes
% are shown, if SHOW is true the contents are shown also.
% UNFOLD(SC,NAME,SHOW)
%R.F. Tap
%15-6-2005, 7-12-2005, 5-1-2006, 3-4-2006
global TXT;
% check input
switch nargin
case 1
Name = inputname(1);
show = true;
case 2
if islogical(varargin{1})
Name = inputname(1);
show = varargin{1};
elseif ischar(varargin{1})
Name = varargin{1};
show = true;
else
error('Second input argument must be a string or a logical')
end
case 3
if ischar(varargin{1})
if islogical(varargin{2})
Name = varargin{1};
show = varargin{2};
else
error('Third input argument must be a logical')
end
else
error('Second input argument must be a string')
end
otherwise
error('Invalid number of input arguments')
end
if isstruct(SC) || isobject(SC)
%number of elements to be displayed
NS = numel(SC);
if show
hmax = NS;
else
hmax = min(1,NS);
end
%recursively display structure including fieldnames
for h=1:hmax
if hmax~=1; error('something weird... MB'); end % MB
F = fieldnames(SC); % MB SC(h) fails on objects...!? so reject hmax>1
NF = length(F);
for i=1:NF
if NS>1
siz = size(SC);
if show
Namei = [Name '(' ind2str(siz,h) ').' F{i}];
else
Namei = [Name '(' ind2str(siz,NS) ').' F{i}];
end
else
Namei = [Name '.' F{i}];
end
if isstruct(SC(h).(F{i}))
unfold(SC(h).(F{i}),Namei,show);
else
if iscell(SC(h).(F{i}))
siz = size(SC(h).(F{i}));
NC = numel(SC(h).(F{i}));
if show
jmax = NC;
else
jmax = 1;
end
for j=1:jmax
if show
Namej = [Namei '{' ind2str(siz,j) '}'];
else
Namej = [Namei '{' ind2str(siz,NC) '}'];
end
if ~isempty(strfind(lower(Namej),lower(TXT)))
disp(Namej)
if show
disp(SC(h).(F{i}){j})
end
end
end
else
if ~isempty(strfind(lower(Namei),lower(TXT)))
disp(Namei)
if show
disp(SC(h).(F{i}))
end
end
end
end
end
end
elseif iscell(SC)
%recursively display cell
siz = size(SC);
for i=1:numel(SC)
Namei = [Name '{' ind2str(siz,i) '}'];
unfold(SC{i},Namei,show);
end
else
warning('something went wrong')
if show
disp(SC)
end
end
%local functions
%--------------------------------------------------------------------------
function str = ind2str(siz,ndx)
n = length(siz);
%treat vectors and scalars correctly
if n==2
if siz(1)==1
siz = siz(2);
n = 1;
elseif siz(2)==1
siz = siz(1);
n = 1;
end
end
k = [1 cumprod(siz(1:end-1))];
ndx = ndx - 1;
str = '';
for i = n:-1:1,
v = floor(ndx/k(i))+1;
if i==n
str = num2str(v);
else
str = [num2str(v) ',' str];
end
ndx = rem(ndx,k(i));
end