-
Notifications
You must be signed in to change notification settings - Fork 0
/
find_data_in_file.m
364 lines (350 loc) · 11.3 KB
/
find_data_in_file.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
function data = find_data_in_file(filename,headerOfData,varargin)
%|
%| function data = find_data_in_file(filename,headerOfData,varargin)
%|
%|=====================================================================================
%| INPUTS:
%| filename [char] name of target file (with extension).
%| headerOfData [char] header of target data
%|
%| varargin:
%| varargin must be given as couple of <[char] input_name, [ ] input_value>.
%|
%| Varargin syntax:
%| ...,[char] input_name ,[ ] input_value,...
%|
%| input_name input_value
%|
%| 'Multi' >> [char] 'on'/'off' :
%| Search target file for multiple data with the same header.
%| Return a vector containing all data found.
%| If 'multi' is 'off' then only the first data found will be
%| returned.
%|
%| 'OutChar' >> [char] 'on'/'off' :
%| Return output as char (instead of as double).
%| Can't be used when 'multi' is 'on' ('multi' will be set
%| 'off').
%|
%| 'IfComposed' >> [cell] {[char] delimiter, [double] whichpart} :
%| Elaborate composed strings.
%| Example: "HeaderOfdata 1234 [unit]"
%| Cell must contain:
%| - [char] delimiter :
%| Specifies the character used to split the data string
%| (in the example, delimiter=' ' leads to
%| [string]("1234","[unit]")).
%| - [double] whichpart :
%| [[integer, positive, vector]]
%| Must be integer and positive and may be a vector.
%| Specifies the part that has to be kept (in the example,
%| whichpart=1 leads to data=1234).
%| It can be a vector in order to keep different parts.
%|
%| 'GoToNewLine' >> [double] linestoskip : [[integer, positive]]
%| Searche data not at the end of HeaderOfdata, but at the
%| beginning of next n lines, where n is linestoskip
%| (if linestoskip==1 then data is considered to be in the
%| next line following the one containing HeaderOfdata).
%|
%| 'HeaderPosition' >> [char] 'left'/'right' :
%| Specify the HeaderOfdata position in the text string read.
%| Default is 'left'.
%|
%| 'SkipFrom' >> [cell] {[char] skipdirection, [double] ntoskip} :
%| Allow to skip ntoskip characters from line start ('left')
%| or from line end ('right') (same direction of
%| ''HeaderPosition'').
%| Cell must contain:
%| - [char] skipdirection : [['left'/'right']]
%| Specify the direction.
%| - [double] ntoskip : [[integer, positive]]
%| Specify the number of lines to be skept.
%| Default is 0.
%| This creates a zone that will be skept by this
%| function.
%|
%| 'TrimData' >> [cell] {[char] trimdirection, [char] whattotrim} :
%| Allow the removal of whattotrim from data in direction
%| specified by 'trimdirection'.
%| Cell must contain:
%| - [char] trimdirection : [['left'/'right/anywhere']]
%| 'left'/'right' : remove 'whattotrim' from left or from
%| right (one time only).
%| 'anywhere' : remove 'whattotrim' anywhere in data
%| string.
%|
%|-------------------------------------------------------------------------------------
%| OUTPUTS:
%| data [double/char] data following HeaderOfdata
%|
%|=====================================================================================
%| EXAMPLES:
%|
%| File loads.txt contains informations about the static margin of an aircraft.
%| Extrapolation of the file content is reported hereafter:
%| line 88|...
%| line 89|STABILITY
%| line 90| Static margin (XN-XCG)/CREF 0.10816
%| line 91|...
%| line 92|...
%| line 93|...
%| In order to obtain the value of the static margin (0.10816), this function can
%| be used with:
%| input:
%| filename = 'loads.txt'
%| headerOfData = ' Static margin (XN-XCG)/CREF';
%|
%| >>data = find_data_in_file(filename,headerOfData,varargin)
%|
%| output:
%| data = 0.10816
%|
%|_____________________________________________________________________________________
%|Author: ni-il
%\_____________________________________________________________________________________
lv=length(varargin);
if rem(lv,2)
error('varargin must be pairs of <''dataname''> and <value>.')
end
flag_multi=0;
flag_outchar=0;
flag_ifcomposed=0;
flag_gotonewline=0;
flag_headeronright=0;
leftskip=0; rightskip=0;
flag_trimdata=0;
for j=1:2:lv
d=varargin{j};
v=varargin{j+1};
switch lower(d)
% Multi
case 'multi'
if ~ischar(v)
error('''multi'' value must be char [''on''/''off''].')
end
switch lower(v)
case 'on'
flag_multi=1;
case 'off'
flag_multi=0;
otherwise
error('''multi'' value must be ''on''/''off''.')
end
% OutChar
case 'outchar'
if ~ischar(v)
error('''outchar'' value must be char [''on''/''off''].')
end
switch lower(v)
case 'on'
flag_outchar=1;
case 'off'
flag_outchar=0;
otherwise
error('''outchar'' value must be ''on''/''off''.')
end
% IfComposed
case 'ifcomposed'
if ~iscell(v)
error('''IfComposed'' value must be {''delimiter'',whichpart}.');
end
flag_ifcomposed=1;
delimiter=v{1};
whichpart=v{2};
if ~ischar(delimiter)
error('In ''IfComposed'' value {''delimiter'',whichpart}, ''delimiter'' must be char.')
end
if ~isnumeric(whichpart)
error('In ''IfComposed'' value {''delimiter'',whichpart}, whichpart must be double.')
end
if rem(whichpart,1) || whichpart<0 %not integer
error('In ''IfComposed'' value {''delimiter'',whichpart}, whichpart must have positive integer value.')
end
% GoToNewLine
case 'gotonewline'
if ~isnumeric(v)
error('''GoToNewLine'' value must be numeric.');
end
if rem(v,1) || v<0 %not integer
error('''GoToNewLine'' value must be positive integer.')
end
flag_gotonewline=1;
linestoskip=v;
% HeaderPosition
case 'headerposition'
if ~ischar(v)
error('''HeaderPosition'' value must be char (''left''/''right'').')
end
switch lower(v)
case 'right'
flag_headeronright=1;
case 'left'
otherwise
error('''HeaderPosition'' value must be ''left''/''right''.')
end
% SkipFrom
case 'skipfrom'
if ~iscell(v)
error('''SkipFrom'' value must be {''skipdirection'',ntoskip}.');
end
skipdirection=v{1};
ntoskip=v{2};
if ~ischar(skipdirection)
error('In ''SkipFrom'' value {''skipdirection'',ntoskip}, ''skipdirection'' must be char.')
end
if ~isnumeric(ntoskip) || rem(ntoskip,1) || ntoskip<0 %not integer
error('In ''SkipFrom'' value {''skipdirection'',ntoskip}, ntoskip must be double and its value must be positive integer.')
end
switch lower(skipdirection)
case 'right'
rightskip=ntoskip;
case 'left'
leftskip=ntoskip;
otherwise
error('In ''SkipFrom'' value {''skipdirection'',ntoskip}, ''skipdirection'' must be ''left''/''right''.')
end
% TrimData
case 'trimdata'
if ~iscell(v)
error('''TrimData'' value must be {''trimdirection'',''whattotrim''}.');
end
flag_trimdata=1;
trimdirection=v{1};
whattotrim=v{2};
if ~ischar(trimdirection)
error('In ''TrimData'' value {''trimdirection'',''whattotrim''}, ''trimdirection'' must be char.')
end
if ~ischar(whattotrim)
error('In ''TrimData'' value {''trimdirection'',''whattotrim''}, ''whattotrim'' must be char.')
end
trimdirection=lower(trimdirection);
if ~strcmp(trimdirection,'left')&&~strcmp(trimdirection,'right')&&~strcmp(trimdirection,'anywhere')
error('In ''TrimData'' value {''trimdirection'',''whattotrim''}, ''trimdirection'' must be ''left''/''right''/''anywhere''.')
end
% otherwise
otherwise
error(['Unrecognized input: ',d,'.']);
end
end
%if nargin==2
% flag_multi=0;
%end
if flag_multi
datavect=[];
end
l=length(headerOfData);
data=NaN;
fid=fopen(filename);
while 1
tline_original = fgetl(fid);
tline_trimmed=tline_original(1+leftskip:end-rightskip);
tline=tline_trimmed;
%disp(tline);
if ~ischar(tline)
break
end
if length(tline)>=l
% check HEADER ('SkipData' included here)
% on LEFT
if ~flag_headeronright
found=strcmp(tline(1:l),headerOfData);
% TODO: partialheader
%if flag_partialheader
% found=0;
% for j=1:length(tline)-l
% if strcmp(tline(j,j+l),headerOfData)
% found=1;
% break
% end
% end
%else
% found=strcmp(tline(1:l),headerOfData);
%end
%
% on RIGHT
else
found=strcmp(tline(end-l+1:end),headerOfData);
%disp(tline(end-l+1:end))
% TODO: partialheader
%if flag_partialheader
% found=0,
% for j=1:length(tline)-l
% if strcmp(tline(end+2-j-l,end+1-j),headerOfData)
% found=1;
% break
% end
% end
%else
% found=strcmp(tline(end-l+1:end),headerOfData);
%end
end
if found
if flag_gotonewline
for j=1:linestoskip
tline = fgetl(fid); %it is not trimmed here
end
dataraw=strtrim(tline);
else
dataraw=strtrim(strrep(tline,headerOfData,''));
end
% trim data
if flag_trimdata
switch trimdirection
case 'left'
for j=1:length(dataraw)
pos=[j:j+length(whattotrim)-1];
if strcmp(dataraw(pos),whattotrim)
dataraw(pos)='';
break
end
end
case 'right'
END=length(dataraw);
for j=1:length(dataraw)
pos=[END+2-j-length(whattotrim):END+1-j];
if strcmp(dataraw(pos),whattotrim)
dataraw(pos)='';
break
end
end
case 'anywhere'
dataraw=strrep(dataraw,whattotrim,'');
end
end
%dataraw to data
if flag_ifcomposed
parts=strsplit(dataraw,delimiter);
if length(whichpart)==1
data=parts{whichpart};
else
data=[];
for j=1:length(whichpart)
data=[data , [parts{whichpart(j)},' ']];
end
end
else
data=dataraw;
end
%
% OUTPUT
if ~flag_outchar
data=str2num( data );
end
if ~flag_multi
break
else
datavect=[datavect;data];
if flag_outchar
warning('Cant have more than first data with "multi" on and "outchar" on.')
break
end
end
end
end
end
fclose(fid);
if flag_multi
data=datavect;
end
end