comparison scripts/io/importdata.m @ 16653:1cfa3a49247f

importdata.m: Added Carriage Return as possible line break character.
author Erik Kjellson <erikiiofph7@users.sourceforge.net>
date Sun, 05 May 2013 19:11:45 +0200
parents 13d1e9bfa362
children 47fbafc6e8e2
comparison
equal deleted inserted replaced
16652:cfacb0446066 16653:1cfa3a49247f
1 ## Copyright (C) 2012 Erik Kjellson 1 ## Copyright (C) 2012-2013 Erik Kjellson <erikiiofph7@users.sourceforge.net>
2 ## 2 ##
3 ## This file is part of Octave. 3 ## This file is part of Octave.
4 ## 4 ##
5 ## Octave is free software; you can redistribute it and/or modify it 5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by 6 ## under the terms of the GNU General Public License as published by
57 ## @end itemize 57 ## @end itemize
58 ## 58 ##
59 ## @seealso{textscan, dlmread, csvread, load} 59 ## @seealso{textscan, dlmread, csvread, load}
60 ## @end deftypefn 60 ## @end deftypefn
61 61
62 ## Author: Erik Kjellson <erikiiofph7@users.sourceforge.net>
63 62
64 function [output, delimiter, header_rows] = importdata (varargin) 63 function [output, delimiter, header_rows] = importdata (varargin)
65 64
66 ## Default values 65 ## Default values
67 fname = ""; 66 fname = "";
191 output.colheaders = []; 190 output.colheaders = [];
192 191
193 ## Read file into string and count the number of header rows 192 ## Read file into string and count the number of header rows
194 file_content = fileread (fname); 193 file_content = fileread (fname);
195 194
196 ## Split the file into rows (using \r\n or \n as delimiters between rows). 195 ## Split the file into rows (using \n and/or \r as delimiters between rows).
197 file_content_rows = regexp (file_content, "\r?\n", "split"); 196 file_content_rows = regexp (file_content, "\n|\n\r|\r|\r\n", "split");
198 197
199 ## FIXME: guess delimiter, if it isn't defined 198 ## FIXME: guess delimiter, if it isn't defined
200 if (isempty (delimiter)) 199 if (isempty (delimiter))
201 error ("importdata: Guessing delimiter is not implemented yet, you have to specify it."); 200 error ("importdata: Guessing delimiter is not implemented yet, you have to specify it.");
202 endif 201 endif
432 %! unlink (fn); 431 %! unlink (fn);
433 %! assert (a, A); 432 %! assert (a, A);
434 %! assert (d, "\t"); 433 %! assert (d, "\t");
435 %! assert (h, 0); 434 %! assert (h, 0);
436 435
436 %!test
437 %! # CR for line breaks
438 %! A = [3.1 -7.2 0; 0.012 6.5 128];
439 %! fn = tmpnam ();
440 %! fid = fopen (fn, "w");
441 %! fputs (fid, "3.1\t-7.2\t0\r0.012\t6.5\t128");
442 %! fclose (fid);
443 %! [a,d,h] = importdata (fn, "\\t");
444 %! unlink (fn);
445 %! assert (a, A);
446 %! assert (d, "\t");
447 %! assert (h, 0);
448