1 | function [S, FileName] = getdata(varargin) |
---|
2 | %GETDATA - Searches through a file (or group of files) for a data structure which matches the family name |
---|
3 | % [Data, FileName] = getdata(FamilyName, FileName) |
---|
4 | % [Data, FileName] = getdata(FamilyName, DeviceList, FileName) |
---|
5 | % |
---|
6 | % INPUTS |
---|
7 | % 1. FamilyName - Family name to search for. This function will search through all |
---|
8 | % the variables in the file looking for a FamilyName match. It will |
---|
9 | % also look through any array or cell array. Arrays within a cell |
---|
10 | % array will also be searched through. |
---|
11 | % 2. FileName - File name to search in (or cell array of file names) {default: []} |
---|
12 | % [] - prompt the user to choose a file |
---|
13 | % 3. DeviceList - Device list to index by (optional input) |
---|
14 | % 4. 'Field' - Sometime searching on FamilyName is not enough. To contraint which |
---|
15 | % Field to search for, use the keyword 'Field' followed |
---|
16 | % by the desired name to look for (see example 3) (optional input). |
---|
17 | % 5. 'Struct' - Return a data structure |
---|
18 | % 'Numeric' - Return numeric outputs {Default} |
---|
19 | % 'Object' - Return a accelerator object (AccObj) |
---|
20 | % |
---|
21 | % OUTPUTS |
---|
22 | % 1. Data - Data found |
---|
23 | % 2. FileName - File name where the data was found (including the path) |
---|
24 | % |
---|
25 | % EXAMPLES |
---|
26 | % 1. Get BPM data in file abc.mat |
---|
27 | % >> BPM = getdata('QF', 'abc.mat'); |
---|
28 | % 2. Get QF setpoint data in file abc.mat |
---|
29 | % >> SP = getdata('QF', 'Struct', 'Field', 'Setpoint', 'abc.mat'); |
---|
30 | % |
---|
31 | % See also getrespmat |
---|
32 | % |
---|
33 | % Written by Greg Portmann |
---|
34 | |
---|
35 | |
---|
36 | % Input parsing |
---|
37 | % [S, FileName] = getdata(Family, 'Field', 'Monitor', FileName, DeviceList) |
---|
38 | % [S, FileName] = getdata(Family, FileName, DeviceList) |
---|
39 | % [S, FileName] = getdata(Family, DeviceList) |
---|
40 | % [S, FileName] = getdata(Family) |
---|
41 | |
---|
42 | |
---|
43 | %%%%%%%%%%%%%%%%% |
---|
44 | % Input Parsing % |
---|
45 | %%%%%%%%%%%%%%%%% |
---|
46 | StructOutputFlag = 0; |
---|
47 | ObjectOutputFlag = 0; |
---|
48 | FileName = ''; |
---|
49 | DeviceList = []; |
---|
50 | FieldConstraint = ''; |
---|
51 | for i = length(varargin):-1:1 |
---|
52 | if isstruct(varargin{i}) |
---|
53 | % Ignor structures |
---|
54 | elseif iscell(varargin{i}) |
---|
55 | % Ignor cells |
---|
56 | elseif strcmpi(varargin{i},'Struct') |
---|
57 | StructOutputFlag = 1; |
---|
58 | varargin(i) = []; |
---|
59 | elseif strcmpi(varargin{i},'Numeric') |
---|
60 | StructOutputFlag = 0; |
---|
61 | varargin(i) = []; |
---|
62 | elseif strcmpi(varargin{i},'Object') |
---|
63 | ObjectOutputFlag = 1; |
---|
64 | StructOutputFlag = 1; |
---|
65 | varargin(i) = []; |
---|
66 | elseif strcmpi(varargin{i},'Field') |
---|
67 | varargin(i) = []; |
---|
68 | FieldConstraint = varargin{i}; |
---|
69 | varargin(i) = []; |
---|
70 | end |
---|
71 | end |
---|
72 | |
---|
73 | |
---|
74 | if isempty(varargin) |
---|
75 | error('FamilyName input required'); |
---|
76 | end |
---|
77 | |
---|
78 | if length(varargin) >= 1 |
---|
79 | Family = varargin{1}; |
---|
80 | end |
---|
81 | |
---|
82 | if length(varargin) >= 2 |
---|
83 | if ischar(varargin{2}) |
---|
84 | FileName = varargin{2}; |
---|
85 | else |
---|
86 | DeviceList = varargin{2}; |
---|
87 | end |
---|
88 | end |
---|
89 | |
---|
90 | if length(varargin) >= 3 |
---|
91 | if ischar(varargin{3}) |
---|
92 | FileName = varargin{3}; |
---|
93 | else |
---|
94 | DeviceList = varargin{3}; |
---|
95 | end |
---|
96 | end |
---|
97 | |
---|
98 | if isempty(FieldConstraint) |
---|
99 | if ismemberof(Family,'BPM') |
---|
100 | FieldConstraint = 'Monitor'; |
---|
101 | else |
---|
102 | FieldConstraint = 'Setpoint'; |
---|
103 | end |
---|
104 | end |
---|
105 | % End input parsing |
---|
106 | |
---|
107 | |
---|
108 | if isempty(FileName) |
---|
109 | [FileName, DirectoryName, FilterIndex] = uigetfile('*.mat', 'Select a Data File'); |
---|
110 | %[FileName, DirectoryName, FilterIndex] = uigetfile('*.mat', 'Select a Data File', getfamilydata('Directory', 'DataRoot')); |
---|
111 | if FilterIndex == 0 |
---|
112 | S=[]; FileName=[]; |
---|
113 | return |
---|
114 | end |
---|
115 | FileName = [DirectoryName FileName]; |
---|
116 | end |
---|
117 | |
---|
118 | |
---|
119 | % Force FileNameCell to be a cell |
---|
120 | if iscell(FileName) |
---|
121 | FileNameCell = FileName; |
---|
122 | else |
---|
123 | FileNameCell = {FileName}; |
---|
124 | end |
---|
125 | |
---|
126 | Data = []; |
---|
127 | Found = 0; |
---|
128 | for i = 1:length(FileNameCell) |
---|
129 | try |
---|
130 | % FileStruct is a structure of all the variable inside FileNameCell |
---|
131 | FileStruct = load(FileNameCell{i}); |
---|
132 | |
---|
133 | % VarCell is a cell array of the field names of FileStruct |
---|
134 | VarCell = fieldnames(FileStruct); |
---|
135 | |
---|
136 | for j = 1:length(VarCell) |
---|
137 | % OneField is one of the fields of FileStruct |
---|
138 | OneField = FileStruct.(VarCell{j}); |
---|
139 | |
---|
140 | % Look through all the variable to find a structure with Monitor, Actuator, and Data fields |
---|
141 | for ii = 1:size(OneField,1) |
---|
142 | for jj = 1:size(OneField,2) |
---|
143 | |
---|
144 | if iscell(OneField) |
---|
145 | |
---|
146 | % If OneField is a cell |
---|
147 | OneCell = OneField{ii,jj}; |
---|
148 | for mm = 1:size(OneCell,1) |
---|
149 | for nn = 1:size(OneCell,2) |
---|
150 | % If OneCell is an array, look through OneCell to find a structure with FamilyName and Data |
---|
151 | if isfield(OneCell(mm,nn),'FamilyName') && isfield(OneCell(mm,nn),'Data') |
---|
152 | if strcmp(OneCell(mm,nn).FamilyName, Family) |
---|
153 | if ~isempty(FieldConstraint) |
---|
154 | if isfield(OneCell(mm,nn), 'Field') |
---|
155 | if strcmp(OneCell(mm,nn).Field, FieldConstraint) |
---|
156 | S = OneCell(mm,nn); |
---|
157 | Found = 1; |
---|
158 | break |
---|
159 | end |
---|
160 | end |
---|
161 | else |
---|
162 | S = OneCell(mm,nn); |
---|
163 | Found = 1; |
---|
164 | break |
---|
165 | end |
---|
166 | end |
---|
167 | |
---|
168 | elseif isfield(OneField(mm,nn), Family) |
---|
169 | % Config data structure |
---|
170 | if isfield(OneCell(mm,nn).(Family),'FamilyName') && isfield(OneCell(mm,nn).(Family),'Data') |
---|
171 | if ~isempty(FieldConstraint) |
---|
172 | if isfield(OneCell(mm,nn).(Family), 'Field') |
---|
173 | if strcmp(OneCell(mm,nn).(Family).Field, FieldConstraint) |
---|
174 | S = OneCell(mm,nn).(Family); |
---|
175 | Found = 1; |
---|
176 | break |
---|
177 | end |
---|
178 | end |
---|
179 | else |
---|
180 | S = OneCell(mm,nn).(Family); |
---|
181 | Found = 1; |
---|
182 | break |
---|
183 | end |
---|
184 | end |
---|
185 | end |
---|
186 | end |
---|
187 | end |
---|
188 | |
---|
189 | else |
---|
190 | % Structure |
---|
191 | % If OneCell is an array, look through OneCell to find a structure with FamilyName and Data |
---|
192 | if isfield(OneField(ii,jj),'FamilyName') && isfield(OneField(ii,jj),'Data') |
---|
193 | if strcmp(OneField(ii,jj).FamilyName, Family) |
---|
194 | if ~isempty(FieldConstraint) |
---|
195 | if isfield(OneField(ii,jj), 'Field') |
---|
196 | if strcmp(OneField(ii,jj).Field, FieldConstraint) |
---|
197 | S = OneField(ii,jj); |
---|
198 | Found = 1; |
---|
199 | break |
---|
200 | end |
---|
201 | end |
---|
202 | else |
---|
203 | S = OneField(ii,jj); |
---|
204 | Found = 1; |
---|
205 | break |
---|
206 | end |
---|
207 | end |
---|
208 | |
---|
209 | elseif isfield(OneField(ii,jj), Family) |
---|
210 | if isfield(OneField(ii,jj).(Family),'FamilyName') && isfield(OneField(ii,jj).(Family),'Data') |
---|
211 | % Old config data structure |
---|
212 | if ~isempty(FieldConstraint) |
---|
213 | if isfield(OneField(ii,jj).(Family), 'Field') |
---|
214 | if strcmp(OneField(ii,jj).(Family).Field, FieldConstraint) |
---|
215 | S = OneField(ii,jj).(Family); |
---|
216 | Found = 1; |
---|
217 | break |
---|
218 | end |
---|
219 | end |
---|
220 | else |
---|
221 | S = OneField(ii,jj).(Family); |
---|
222 | Found = 1; |
---|
223 | break |
---|
224 | end |
---|
225 | elseif isfield(OneField(ii,jj), Family) && isfield(OneField(ii,jj).(Family),FieldConstraint) |
---|
226 | % New config data structure |
---|
227 | if ~isempty(FieldConstraint) |
---|
228 | if isfield(OneField(ii,jj).(Family), FieldConstraint) |
---|
229 | if isfield(OneField(ii,jj).(Family).(FieldConstraint),'FamilyName') && isfield(OneField(ii,jj).(Family).(FieldConstraint),'Data') |
---|
230 | S = OneField(ii,jj).(Family).(FieldConstraint); |
---|
231 | Found = 1; |
---|
232 | break |
---|
233 | end |
---|
234 | end |
---|
235 | end |
---|
236 | end |
---|
237 | end |
---|
238 | end |
---|
239 | end |
---|
240 | if Found |
---|
241 | break; |
---|
242 | end |
---|
243 | end |
---|
244 | if Found |
---|
245 | break; |
---|
246 | end |
---|
247 | end |
---|
248 | catch |
---|
249 | end |
---|
250 | if Found |
---|
251 | break; |
---|
252 | end |
---|
253 | end |
---|
254 | |
---|
255 | if ~Found |
---|
256 | error('Could not find the data structure'); |
---|
257 | end |
---|
258 | |
---|
259 | |
---|
260 | % If a DeviceList exists, index .Data, .Status, and .DeviceList |
---|
261 | if ~isempty(DeviceList) |
---|
262 | if size(DeviceList,2) == 1 |
---|
263 | % Convert element list to a device list |
---|
264 | DeviceList = elem2dev(Family, DeviceList); |
---|
265 | end |
---|
266 | |
---|
267 | DeviceListTotal = S.DeviceList; |
---|
268 | [Index, iNotFound, iMonitor] = findrowindex(DeviceList, DeviceListTotal); |
---|
269 | if ~isempty(iNotFound) |
---|
270 | % Error if a monitor is not found |
---|
271 | %for i = iNotFound(:)' |
---|
272 | % fprintf(' %s(%d,%d) not found\n', S.FamilyName, DeviceList(i,1), DeviceList(i,2)); |
---|
273 | %end |
---|
274 | %error(sprintf('Not all the devices found in %s', FileName)); |
---|
275 | |
---|
276 | % Fill the missing .Data with NaN |
---|
277 | Data = NaN * ones(size(DeviceList,1), size(S.Data,2)); |
---|
278 | Data(iMonitor,:) = S.Data(Index,:); |
---|
279 | S.Data = Data; |
---|
280 | |
---|
281 | % Fill the missing .Status with zeros |
---|
282 | if length(S.Status) == 1 |
---|
283 | S.Status = S.Status * ones(size(S.Data,1),1); |
---|
284 | end |
---|
285 | Data = zeros(size(DeviceList,1), 1); |
---|
286 | Data(iMonitor,:) = S.Status(Index,:); |
---|
287 | S.Status = Data; |
---|
288 | else |
---|
289 | S.Data = S.Data(Index, :); |
---|
290 | S.Status = S.Status(Index, :); |
---|
291 | end |
---|
292 | |
---|
293 | % Index the device list |
---|
294 | S.DeviceList = DeviceList; |
---|
295 | |
---|
296 | |
---|
297 | % Find other fields that need to be indexed |
---|
298 | SFields = fieldnames(S); |
---|
299 | for i = 1:length(SFields) |
---|
300 | if size(S.(SFields{i}),1) == size(DeviceListTotal,1) |
---|
301 | if ~isempty(iNotFound) |
---|
302 | Data = NaN * ones(size(DeviceList,1), size(S.(SFields{i}),2)); |
---|
303 | Data(iMonitor,:) = S.(SFields{i})(Index,:); |
---|
304 | S.(SFields{i}) = Data; |
---|
305 | else |
---|
306 | % Fill the missing .Data with NaN |
---|
307 | Data = NaN * ones(size(DeviceList,1), size(S.(SFields{i}),2)); |
---|
308 | Data(iMonitor,:) = S.(SFields{i})(Index,:); |
---|
309 | S.(SFields{i}) = Data; |
---|
310 | |
---|
311 | %S.(SFields{i}) = S.(SFields{i})(Index,:); |
---|
312 | end |
---|
313 | end |
---|
314 | end |
---|
315 | end |
---|
316 | |
---|
317 | |
---|
318 | % Make the output an AccObj object |
---|
319 | if ObjectOutputFlag |
---|
320 | Data = AccObj(Data); |
---|
321 | end |
---|
322 | |
---|
323 | |
---|
324 | if ~StructOutputFlag |
---|
325 | S = S.Data; |
---|
326 | end |
---|
327 | |
---|