Mercurial > hg > octave-nkf
comparison scripts/io/textread.m @ 20038:9fc020886ae9
maint: Clean up m-files to follow Octave coding conventions.
Try to trim long lines to < 80 chars.
Use '##' for single line comments.
Use '(...)' around tests for if/elseif/switch/while.
Abut cell indexing operator '{' next to variable.
Abut array indexing operator '(' next to variable.
Use space between negation operator '!' and following expression.
Use two newlines between endfunction and start of %!test or %!demo code.
Remove unnecessary parens grouping between short-circuit operators.
Remove stray extra spaces (typos) between variables and assignment operators.
Remove stray extra spaces from ends of lines.
author | Rik <rik@octave.org> |
---|---|
date | Mon, 23 Feb 2015 14:54:39 -0800 |
parents | 4197fc428c7d |
children | e51473fdb622 |
comparison
equal
deleted
inserted
replaced
20037:a1acca0c2216 | 20038:9fc020886ae9 |
---|---|
116 ## 'endofline' option set by user. | 116 ## 'endofline' option set by user. |
117 if (! ischar (varargin{endofline + 1})); | 117 if (! ischar (varargin{endofline + 1})); |
118 error ("character value required for EndOfLine"); | 118 error ("character value required for EndOfLine"); |
119 endif | 119 endif |
120 else | 120 else |
121 ## Determine EOL from file. Search for EOL candidates in first BUFLENGTH chars | 121 ## Determine EOL from file. |
122 ## Search for EOL candidates in the first BUFLENGTH chars | |
122 eol_srch_len = min (length (str), BUFLENGTH); | 123 eol_srch_len = min (length (str), BUFLENGTH); |
123 ## First try DOS (CRLF) | 124 ## First try DOS (CRLF) |
124 if (! isempty (strfind (str(1 : eol_srch_len), "\r\n"))) | 125 if (! isempty (strfind (str(1 : eol_srch_len), "\r\n"))) |
125 eol_char = "\r\n"; | 126 eol_char = "\r\n"; |
126 ## Perhaps old Macintosh? (CR) | 127 ## Perhaps old Macintosh? (CR) |
156 ++nblks; | 157 ++nblks; |
157 endif | 158 endif |
158 endwhile | 159 endwhile |
159 ## Found EOL delimiting last requested line. Compute ptr (incl. EOL) | 160 ## Found EOL delimiting last requested line. Compute ptr (incl. EOL) |
160 if (isempty (eoi)) | 161 if (isempty (eoi)) |
161 printf ("textread: format repeat count specified but no endofline found\n"); | 162 disp ("textread: format repeat count specified but no endofline found"); |
162 eoi_pos = nblks * BUFLENGTH + count; | 163 eoi_pos = nblks * BUFLENGTH + count; |
163 else | 164 else |
164 eoi_pos = (nblks * BUFLENGTH) + eoi(end + min (nlines, n_eoi) - n_eoi); | 165 eoi_pos = (nblks * BUFLENGTH) + eoi(end + min (nlines, n_eoi) - n_eoi); |
165 endif | 166 endif |
166 fseek (fid, st_pos, "bof"); | 167 fseek (fid, st_pos, "bof"); |
175 if (isempty (find (strcmpi ("whitespace", varargin)))) | 176 if (isempty (find (strcmpi ("whitespace", varargin)))) |
176 varargin(end+1:end+2) = {"whitespace", " \b\t"}; | 177 varargin(end+1:end+2) = {"whitespace", " \b\t"}; |
177 endif | 178 endif |
178 | 179 |
179 ## Call strread to make it do the real work | 180 ## Call strread to make it do the real work |
180 [varargout{1:max (nargout, 1)}] = strread (str, format, varargin {:}); | 181 [varargout{1:max (nargout, 1)}] = strread (str, format, varargin{:}); |
181 | 182 |
182 ## Hack to concatenate/reshape numeric output into 2D array (undocumented ML) | 183 ## Hack to concatenate/reshape numeric output into 2D array (undocumented ML) |
183 ## In ML this only works in case of an empty format string | 184 ## In ML this only works in case of an empty format string |
184 if (isempty (format)) | 185 if (isempty (format)) |
185 ## Get number of fields per line. | 186 ## Get number of fields per line. |
200 str = strtrim (str); | 201 str = strtrim (str); |
201 ## 5A. Count spaces, add one to get nr of data fields per line | 202 ## 5A. Count spaces, add one to get nr of data fields per line |
202 ncols = numel (strfind (str, " ")) + 1; | 203 ncols = numel (strfind (str, " ")) + 1; |
203 else | 204 else |
204 ## 3B. Just count delimiters. FIXME: delimiters could occur in literals | 205 ## 3B. Just count delimiters. FIXME: delimiters could occur in literals |
205 delimiter = varargin {idelimiter+1}; | 206 delimiter = varargin{idelimiter+1}; |
206 ncols = numel (regexp (str, sprintf ("[%s]", delimiter))) + 1; | 207 ncols = numel (regexp (str, sprintf ("[%s]", delimiter))) + 1; |
207 endif | 208 endif |
208 ## 6. Reshape; watch out, we need a transpose | 209 ## 6. Reshape; watch out, we need a transpose |
209 nrows = ceil (numel (varargout{1}) / ncols); | 210 nrows = ceil (numel (varargout{1}) / ncols); |
210 pad = mod (numel (varargout{1}), ncols); | 211 pad = mod (numel (varargout{1}), ncols); |
236 %! dlmwrite (f, d, "precision", "%5.2f"); | 237 %! dlmwrite (f, d, "precision", "%5.2f"); |
237 %! [a, b] = textread (f, "%f, %f", "headerlines", 1); | 238 %! [a, b] = textread (f, "%f, %f", "headerlines", 1); |
238 %! unlink (f); | 239 %! unlink (f); |
239 %! assert (a, d(2:7, 1), 1e-2); | 240 %! assert (a, d(2:7, 1), 1e-2); |
240 | 241 |
241 %% Test reading 2D matrix with empty format | 242 ## Test reading 2D matrix with empty format |
242 %!test | 243 %!test |
243 %! f = tempname (); | 244 %! f = tempname (); |
244 %! d = rand (5, 2); | 245 %! d = rand (5, 2); |
245 %! dlmwrite (f, d, "precision", "%5.2f"); | 246 %! dlmwrite (f, d, "precision", "%5.2f"); |
246 %! A = textread (f, "", "headerlines", 3); | 247 %! A = textread (f, "", "headerlines", 3); |
247 %! unlink (f); | 248 %! unlink (f); |
248 %! assert (A, d(4:5, :), 1e-2); | 249 %! assert (A, d(4:5, :), 1e-2); |
249 | 250 |
250 %% Read multiple lines using empty format string | 251 ## Read multiple lines using empty format string |
251 %!test | 252 %!test |
252 %! f = tempname (); | 253 %! f = tempname (); |
253 %! unlink (f); | 254 %! unlink (f); |
254 %! fid = fopen (f, "w"); | 255 %! fid = fopen (f, "w"); |
255 %! d = rand (1, 4); | 256 %! d = rand (1, 4); |
257 %! fclose (fid); | 258 %! fclose (fid); |
258 %! A = textread (f, ""); | 259 %! A = textread (f, ""); |
259 %! unlink (f); | 260 %! unlink (f); |
260 %! assert (A, d, 1e-6); | 261 %! assert (A, d, 1e-6); |
261 | 262 |
262 %% Empty format, corner case = one line w/o EOL | 263 ## Empty format, corner case = one line w/o EOL |
263 %!test | 264 %!test |
264 %! f = tempname (); | 265 %! f = tempname (); |
265 %! unlink (f); | 266 %! unlink (f); |
266 %! fid = fopen (f, "w"); | 267 %! fid = fopen (f, "w"); |
267 %! d = rand (1, 4); | 268 %! d = rand (1, 4); |
269 %! fclose (fid); | 270 %! fclose (fid); |
270 %! A = textread (f, ""); | 271 %! A = textread (f, ""); |
271 %! unlink (f); | 272 %! unlink (f); |
272 %! assert (A, d, 1e-6); | 273 %! assert (A, d, 1e-6); |
273 | 274 |
274 %% Read multiple lines using empty format string, missing data (should be 0) | 275 ## Read multiple lines using empty format string, missing data (should be 0) |
275 %!test | 276 %!test |
276 %! f = tempname (); | 277 %! f = tempname (); |
277 %! unlink (f); | 278 %! unlink (f); |
278 %! fid = fopen (f, "w"); | 279 %! fid = fopen (f, "w"); |
279 %! d = rand (1, 4); | 280 %! d = rand (1, 4); |
281 %! fclose (fid); | 282 %! fclose (fid); |
282 %! A = textread (f, ""); | 283 %! A = textread (f, ""); |
283 %! unlink (f); | 284 %! unlink (f); |
284 %! assert (A, [ d(1:2) 0 d(3:4)], 1e-6); | 285 %! assert (A, [ d(1:2) 0 d(3:4)], 1e-6); |
285 | 286 |
286 %% Test with empty positions - ML returns 0 for empty fields | 287 ## Test with empty positions - ML returns 0 for empty fields |
287 %!test | 288 %!test |
288 %! f = tempname (); | 289 %! f = tempname (); |
289 %! unlink (f); | 290 %! unlink (f); |
290 %! fid = fopen (f, "w"); | 291 %! fid = fopen (f, "w"); |
291 %! d = rand (1, 4); | 292 %! d = rand (1, 4); |
293 %! fclose (fid); | 294 %! fclose (fid); |
294 %! A = textread (f, "", "delimiter", ","); | 295 %! A = textread (f, "", "delimiter", ","); |
295 %! unlink (f); | 296 %! unlink (f); |
296 %! assert (A, [0 2 0 4; 5 0 7 0], 1e-6); | 297 %! assert (A, [0 2 0 4; 5 0 7 0], 1e-6); |
297 | 298 |
298 %% Another test with empty format + positions, now with more incomplete lower | 299 ## Another test with empty format + positions, now with more incomplete lower |
299 %% row (must be appended with zeros to get rectangular matrix) | 300 ## row (must be appended with zeros to get rectangular matrix) |
300 %!test | 301 %!test |
301 %! f = tempname (); | 302 %! f = tempname (); |
302 %! unlink (f); | 303 %! unlink (f); |
303 %! fid = fopen (f, "w"); | 304 %! fid = fopen (f, "w"); |
304 %! d = rand (1, 4); | 305 %! d = rand (1, 4); |
306 %! fclose (fid); | 307 %! fclose (fid); |
307 %! A = textread (f, "", "delimiter", ","); | 308 %! A = textread (f, "", "delimiter", ","); |
308 %! unlink (f); | 309 %! unlink (f); |
309 %! assert (A, [0 2 0 4; 5 0 0 0], 1e-6); | 310 %! assert (A, [0 2 0 4; 5 0 0 0], 1e-6); |
310 | 311 |
311 %% Test input validation | 312 ## Test input validation |
312 %!error textread () | 313 %!error textread () |
313 %!error textread (1) | 314 %!error textread (1) |
314 %!error <arguments must be strings> textread (1, "%f") | 315 %!error <arguments must be strings> textread (1, "%f") |
315 %!error <arguments must be strings> textread ("fname", 1) | 316 %!error <arguments must be strings> textread ("fname", 1) |
316 %!error <missing or illegal value for> textread (file_in_loadpath ("textread.m"), "", "headerlines") | 317 %!error <missing or illegal value for> textread (file_in_loadpath ("textread.m"), "", "headerlines") |