-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfun_read_file.m
More file actions
276 lines (269 loc) · 9.26 KB
/
fun_read_file.m
File metadata and controls
276 lines (269 loc) · 9.26 KB
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
function [OUTPUT] = fun_read_file(PFILENAME)
% fun_read_file
%
% ***********************************************************************
% *** read data file ****************************************************
% ***********************************************************************
%
% fun_read_file(PFILENAME)
% loads in a data file and returns a cell array of filtered content
%
% ***********************************************************************
% *** HISTORY ***********************************************************
% ***********************************************************************
%
% 20/12/29: CREATED
% 21/02/25: added more fileformat flexibility and filtering
% 22/05/10: added ability to accept special MATLAB scatter shape codes
%
% ***********************************************************************
% *********************************************************************** %
% *** INITIALIZE PARAMETERS & VARIABLES ********************************* %
% *********************************************************************** %
%
% *** INITIALIZE ******************************************************** %
%
% set version!
par_ver = 0.01;
% set function name
str_function = mfilename;
str_function(find(str_function(:)=='_')) = '-';
% set date
str_date = [datestr(date,11), datestr(date,5), datestr(date,7)];
% determine whcih stupid version of MUTLAB we are using
tmp_mutlab = version('-release');
str_mutlab = tmp_mutlab(1:4);
par_mutlab = str2num(str_mutlab);
% define special characters (e.g. for specifing marker shapes) -- *<>^
% NOTE: '+' becasue it is used in the scientific notation ...
str_special = [char(42) char(60) char(62) char(94)];
%
% *** copy passed parameters ******************************************** %
%
% set passed parameters
str_filename = PFILENAME;
%
% *********************************************************************** %
% *********************************************************************** %
% *** PROCESS THE STUPID FILE ******************************************* %
% *********************************************************************** %
%
% *** initial load ****************************************************** %
%
% read entire file contents; split into lines
loc_file = fileread(str_filename);
loc_file = regexp(loc_file, '\r\n', 'split');
% determine number of lines
n_lines = length(loc_file);
% if file still not split (n_lines == 1), try again ...
if (n_lines == 1)
loc_file = splitlines(loc_file);
end
% determine number of lines
n_lines = length(loc_file);
% convert to column oriented cell
loc_size = size(loc_file);
if (loc_size(1) == 1)
loc_file = loc_file';
end
%
% *** find and process comment lines ************************************ %
%
% create vector for storing comment line numbers
v_comment = [];
% look for comment lines (lines starting with '%')
% NOTE: test whether the '%' occurs at position 1 ...
% (could also allow for it to be further along)
for n = 1:n_lines
loc_comment = strfind(loc_file{n},'%');
if ~isempty(loc_comment)
if (loc_comment(1) == 1)
v_comment = [v_comment n];
end
end
end
% remove comment lines
if ~isempty(v_comment)
disp([' * ACTION: Marking comment lines #: ' num2str(v_comment) ' for removal.']);
loc_file(v_comment,:) = [];
end
% update number of lines
n_lines = length(loc_file);
% remove any blank line at end
n = n_lines;
loc_line = loc_file{n};
if (isempty(loc_line))
loc_file(n,:) = [];
end
% update number of lines
n_lines = length(loc_file);
%
% *** parse delimanators ************************************************ %
%
% replace all deliminator characters by a single space
% NOTE:
% 009 == TAB
% 032 == SPACE
% 035 == #
% 036 == $
% 037 == %
% 038 == &
% 044 == ,
% loop through all lines of text
for n = 1:n_lines
% determine format from first line
loc_line = loc_file{n};
% replace any tabs with spaces
loc_line = strrep(loc_line,char(9),char(32));
% replace any commas with spaces
loc_line = strrep(loc_line,char(44),char(32));
% replace any single/double like quotation marks with spaces
loc_line = strrep(loc_line,char(34),char(32));
loc_line = strrep(loc_line,char(39),char(32));
loc_line = strrep(loc_line,char(96),char(32));
loc_line = strrep(loc_line,char(145),char(32));
loc_line = strrep(loc_line,char(146),char(32));
loc_line = strrep(loc_line,char(147),char(32));
loc_line = strrep(loc_line,char(148),char(32));
% replace any % characters with spaces
loc_line = strrep(loc_line,char(37),char(32));
% now reduce any multiple spaces to a single space
delimreplace=true;
while (delimreplace == true)
doublespace = numel(strfind(loc_line,[char(32) char(32)]));
if (doublespace > 0)
loc_line = strrep(loc_line,[char(32) char(32)],char(32));
else
delimreplace=false;
end
end
% remove any spaces at the very start and end of the lines
loc_spaces = strfind(loc_line,char(32));
if (loc_spaces(end) == length(loc_line)), loc_line(end) = []; end
if (loc_spaces(1) == 1), loc_line(1) = []; end
% replace entry
loc_file{n} = loc_line;
end
%
% *** determine format ************************************************** %
%
% work on the basis that the first line of data is 'correct'
loc_line = loc_file{1};
% deduce number of columns from first line
% (remember that colums == seperators + 1)
if (numel(strfind(loc_line,char(32))) > 0)
loc_spaces = strfind(loc_line,char(32));
n_space = numel(loc_spaces);
n_columns = n_space + 1;
else
disp([' * ERROR: Cannot determine data file format.']);
disp([' * ACTION: EXITING ...']);
disp([' ']);
return;
end
% deduce format from first line
% NOTE: loc_alpha(1) is assuming that if the first element of the string is a character, all is OK ...
v_text = regexp(loc_line, ' ', 'split');
v_format = [];
for n = 1:n_columns
loc_txt = v_text{n};
loc_alpha = isstrprop(loc_txt,'alpha');
loc_char = intersect(loc_txt,str_special);
v_format = [v_format (loc_alpha(1) || ~isempty(loc_char))];
end
%
% *** filter out lines that do not conform to the first ***************** %
%
% (1) MISSING ENTRIES
% create vector for storing non-confirming lines
v_nonconform = [];
% look for non-conforming lines
for n = 1:n_lines
loc_line = loc_file{n};
loc_space = numel(strfind(loc_line,char(32)));
if (loc_space ~= n_space)
v_nonconform = [v_nonconform n];
end
end
% remove non-conforming lines
if ~isempty(v_nonconform)
disp([' * WARNING: Each row must have the same number of entries.']);
disp([' * ACTION: Deleting non-confirming rows: #' num2str(v_nonconform)]);
loc_file(v_nonconform,:) = [];
end
% update number of lines
n_lines = length(loc_file);
%
% (2) INCOMPATABLE DATA TYPES
% create vector for storing non-confirming lines
v_nonconform = [];
% look for non-conforming lines
for n = 1:n_lines
loc_line = loc_file{n};
v_text = regexp(loc_line, ' ', 'split');
% create format vector
loc_format = [];
for m = 1:n_columns
loc_txt = v_text{m};
loc_alpha = isstrprop(loc_txt,'alpha');
loc_char = intersect(loc_txt,str_special);
loc_format = [loc_format (loc_alpha(1) || ~isempty(loc_char))];
end
if (~isequal(v_format,loc_format))
v_nonconform = [v_nonconform n];
end
end
% remove non-conforming lines
if ~isempty(v_nonconform)
disp([' * WARNING: Each column must have row entries of the same type (number or string).']);
disp([' * ACTION: Deleting non-confirming rows: #' num2str(v_nonconform)]);
loc_file(v_nonconform,:) = [];
end
% update number of lines
n_lines = length(loc_file);
%
% *********************************************************************** %
% *********************************************************************** %
% *** SAVE A COPY OF THE PROCESSED DATA ********************************* %
% *********************************************************************** %
%
% save copy of modified file
filename = [str_filename '.' str_date '.dat'];
fid = fopen(filename, 'wt');
fprintf(fid, '%s\n', loc_file{:});
fclose(fid);
%
% *********************************************************************** %
% *********************************************************************** %
% *** FUNCTION RETURN *************************************************** %
% *********************************************************************** %
%
% reconfigure cell array
c_data = cell(n_lines,n_columns);
for n = 1:n_lines
loc_line = loc_file{n};
v_text = regexp(loc_line, ' ', 'split');
for m = 1:n_columns
loc_txt = v_text{m};
if v_format(m)
c_data(n,m) = {loc_txt};
else
c_data{n,m} = str2double(loc_txt);
end
end
end
% create structure for data return
output.cdata = c_data;
output.vformat = v_format;
% set returned data
OUTPUT = output;
%
% *********************************************************************** %
% *********************************************************************** %
% *** END *************************************************************** %
% *********************************************************************** %
%
% END
%%%disp(['END ...'])
%
% *********************************************************************** %