Mercurial > octave-nkf
comparison scripts/io/importdata.m @ 18697:271873b2f44f
importdata.m: Return cellstr of text when no numeric data found (bug #41630).
* importdata.m: If no numeric data found, read in entire file as chars.
Use strsplit with EOL as delimiter to return cellstr of lines.
author | Philip Nienhuis <prnienhuis@users.sf.net> |
---|---|
date | Wed, 02 Apr 2014 20:46:57 +0200 |
parents | fcd87f68af4f |
children | 99d1ef340de4 |
comparison
equal
deleted
inserted
replaced
18696:1fc22871bd8b | 18697:271873b2f44f |
---|---|
207 break; | 207 break; |
208 endif | 208 endif |
209 | 209 |
210 endwhile | 210 endwhile |
211 | 211 |
212 fclose (fid); | |
213 | |
214 if (row == -1) | 212 if (row == -1) |
215 error ("importdata: Unable to determine delimiter"); | 213 ## No numeric data found => return file as cellstr array |
216 endif | 214 ## 1. Read as char string |
215 fseek (fid, 0, "bof"); | |
216 output = fread (fid, Inf, "*char")'; | |
217 fclose (fid); | |
218 ## 2. Find EOL type | |
219 idx = find (output == "\n", 1) - 1; | |
220 if (isindex (idx) && output(idx) == "\r") | |
221 dlm = "\r\n"; | |
222 else | |
223 dlm = "\n"; | |
224 endif | |
225 ## 3. Split each line into a cell (column vector) | |
226 output = strsplit (output, dlm)'; | |
227 ## 4. Remove last cell (for files with -proper- EOL before EOF) | |
228 if (isempty (output{end})) | |
229 output(end) = []; | |
230 endif | |
231 ## 5. Return after setting some output data | |
232 delimiter = ""; | |
233 header_rows = numel (output); | |
234 return; | |
235 else | |
236 fclose (fid); | |
237 endif | |
238 | |
217 if (num_header_rows >= 0) | 239 if (num_header_rows >= 0) |
218 header_rows = num_header_rows; | 240 header_rows = num_header_rows; |
219 endif | 241 endif |
220 | 242 |
221 ## Now, let the efficient built-in routine do the bulk of the work. | 243 ## Now, let the efficient built-in routine do the bulk of the work. |
495 %! unlink (fn); | 517 %! unlink (fn); |
496 %! assert (a, A); | 518 %! assert (a, A); |
497 %! assert (d, "\t"); | 519 %! assert (d, "\t"); |
498 %! assert (h, 0); | 520 %! assert (h, 0); |
499 | 521 |
522 %!test | |
523 %! ## Only text / no numeric data; \n as EOL | |
524 %! fn = tmpnam (); | |
525 %! fid = fopen (fn, "w"); | |
526 %! fputs (fid, "aaaa 11\nbbbbb 22\nccccc 3\n"); | |
527 %! fclose (fid); | |
528 %! [a, d, h] = importdata (fn); | |
529 %! unlink (fn); | |
530 %! assert (a, {"aaaa 11"; "bbbbb 22"; "ccccc 3"}); | |
531 %! assert (d, ""); | |
532 %! assert (h, 3); | |
533 | |
534 %!test | |
535 %! ## Only text / no numeric data; \r\n as EOL; missing last EOL before EOF | |
536 %! fn = tmpnam (); | |
537 %! fid = fopen (fn, "w"); | |
538 %! fputs (fid, "aaaa 11\r\nbbbbb 22\r\nccccc 3"); | |
539 %! fclose (fid); | |
540 %! [a, d, h] = importdata (fn); | |
541 %! unlink (fn); | |
542 %! assert (a, {"aaaa 11"; "bbbbb 22"; "ccccc 3"}); | |
543 %! assert (d, ""); | |
544 %! assert (h, 3); | |
545 | |
500 %!error importdata () | 546 %!error importdata () |
501 %!error importdata (1,2,3,4) | 547 %!error importdata (1,2,3,4) |
502 %!error <FNAME must be a string> importdata (1) | 548 %!error <FNAME must be a string> importdata (1) |
503 %!error <option -pastespecial not implemented> importdata ("-pastespecial") | 549 %!error <option -pastespecial not implemented> importdata ("-pastespecial") |
504 %!error <DELIMITER must be a single character> importdata ("foo", 1) | 550 %!error <DELIMITER must be a single character> importdata ("foo", 1) |