changeset 18663: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 1fc22871bd8b
children 900b524d9072
files scripts/io/importdata.m
diffstat 1 files changed, 50 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/scripts/io/importdata.m	Fri Apr 18 19:26:22 2014 -0700
+++ b/scripts/io/importdata.m	Wed Apr 02 20:46:57 2014 +0200
@@ -209,11 +209,33 @@
 
   endwhile
 
-  fclose (fid);
+  if (row == -1)
+    ## No numeric data found => return file as cellstr array
+    ## 1. Read as char string
+    fseek (fid, 0, "bof");
+    output = fread (fid, Inf, "*char")';
+    fclose (fid);
+    ## 2. Find EOL type
+    idx = find (output == "\n", 1) - 1;
+    if (isindex (idx) && output(idx) == "\r")
+      dlm = "\r\n";
+    else
+      dlm = "\n";
+    endif
+    ## 3. Split each line into a cell (column vector)
+    output = strsplit (output, dlm)';
+    ## 4. Remove last cell (for files with -proper- EOL before EOF)
+    if (isempty (output{end}))
+      output(end) = [];
+    endif
+    ## 5. Return after setting some output data
+    delimiter = "";
+    header_rows = numel (output);
+    return;
+  else
+    fclose (fid);
+  endif
 
-  if (row == -1)
-    error ("importdata: Unable to determine delimiter");
-  endif
   if (num_header_rows >= 0)
     header_rows = num_header_rows;
   endif
@@ -497,6 +519,30 @@
 %! assert (d, "\t");
 %! assert (h, 0);
 
+%!test
+%! ## Only text / no numeric data; \n as EOL
+%! fn  = tmpnam ();
+%! fid = fopen (fn, "w");
+%! fputs (fid, "aaaa 11\nbbbbb 22\nccccc 3\n");
+%! fclose (fid);
+%! [a, d, h] = importdata (fn);
+%! unlink (fn);
+%! assert (a, {"aaaa 11"; "bbbbb 22"; "ccccc 3"});
+%! assert (d, "");
+%! assert (h, 3);
+
+%!test
+%! ## Only text / no numeric data; \r\n as EOL; missing last EOL before EOF
+%! fn  = tmpnam ();
+%! fid = fopen (fn, "w");
+%! fputs (fid, "aaaa 11\r\nbbbbb 22\r\nccccc 3");
+%! fclose (fid);
+%! [a, d, h] = importdata (fn);
+%! unlink (fn);
+%! assert (a, {"aaaa 11"; "bbbbb 22"; "ccccc 3"});
+%! assert (d, "");
+%! assert (h, 3);
+
 %!error importdata ()
 %!error importdata (1,2,3,4)
 %!error <FNAME must be a string> importdata (1)