52 if (is.peek () == std::istream::traits_type::eof ())
56 if (::isalpha (is.peek ()))
59 while (is && ::isalpha (is.peek ()))
95 if (ch ==
'.' || ch ==
':')
115 if (ch ==
'.' || ch ==
':')
121 if (!is || ch !=
'.')
132 if (!is || !is.eof ())
138 if (stat && is && !is.eof ())
141 if (!is || !is.eof ())
159 DEFUN (dlmread, args, ,
161 @deftypefn {Built-in Function} {@var{data} =} dlmread (@var{file})\n\
162 @deftypefnx {Built-in Function} {@var{data} =} dlmread (@var{file}, @var{sep})\n\
163 @deftypefnx {Built-in Function} {@var{data} =} dlmread (@var{file}, @var{sep}, @var{r0}, @var{c0})\n\
164 @deftypefnx {Built-in Function} {@var{data} =} dlmread (@var{file}, @var{sep}, @var{range})\n\
165 @deftypefnx {Built-in Function} {@var{data} =} dlmread (@dots{}, \"emptyvalue\", @var{EMPTYVAL})\n\
166 Read the matrix @var{data} from a text file. If not defined the separator\n\
167 between fields is determined from the file itself. Otherwise the\n\
168 separation character is defined by @var{sep}.\n\
170 Given two scalar arguments @var{r0} and @var{c0}, these define the starting\n\
171 row and column of the data to be read. These values are indexed from zero,\n\
172 such that the first row corresponds to an index of zero.\n\
174 The @var{range} parameter may be a 4-element vector containing the upper\n\
175 left and lower right corner @code{[@var{R0},@var{C0},@var{R1},@var{C1}]}\n\
176 where the lowest index value is zero. Alternatively, a spreadsheet style\n\
177 range such as @qcode{\"A2..Q15\"} or @qcode{\"T1:AA5\"} can be used. The\n\
178 lowest alphabetical index @qcode{'A'} refers to the first column. The\n\
179 lowest row index is 1.\n\
181 @var{file} should be a file name or file id given by @code{fopen}. In the\n\
182 latter case, the file is read until end of file is reached.\n\
184 The @qcode{\"emptyvalue\"} option may be used to specify the value used to\n\
185 fill empty fields. The default is zero.\n\
186 @seealso{csvread, textscan, textread, dlmwrite}\n\
191 int nargin = args.
length ();
193 double empty_value = 0.0;
195 if (nargin > 2 && args(nargin-2).is_string ()
196 && args(nargin-2).string_value () ==
"emptyvalue")
198 empty_value = args(nargin-1).double_value ();
204 if (nargin < 1 || nargin > 4)
210 std::istream *
input = 0;
211 std::ifstream input_file;
213 if (args(0).is_string ())
216 std::string fname (args(0).string_value ());
222 input_file.open (tname.c_str (), std::ios::in);
226 error (
"dlmread: unable to open file '%s'", fname.c_str ());
232 else if (args(0).is_scalar_type ())
243 error (
"dlmread: stream FILE not open for input");
249 error (
"dlmread: FILE argument must be a string or file id");
257 if (args(1).is_sq_string ())
260 sep = args(1).string_value ();
273 error (
"dlmread: error parsing RANGE");
275 else if (nargin == 4)
277 r0 = args(2).idx_type_value ();
278 c0 = args(3).idx_type_value ();
284 if (r0 < 0 || c0 < 0)
285 error (
"dlmread: left & top must be positive");
295 bool iscmplx =
false;
296 bool sepflag =
false;
302 getline (*input, line);
305 std::istringstream tmp_stream;
309 while (getline (*input, line))
312 if (line.find_first_not_of (
" \t") == std::string::npos)
319 size_t n = line.find_first_of (
",:; \t",
320 line.find_first_of (
"0123456789"));
321 if (n == std::string::npos)
328 char ch = line.at (n);
349 size_t pos1 = line.find_first_not_of (
" \t");
352 size_t pos2 = line.find_first_of (sep, pos1);
354 if (sepflag && pos2 != std::string::npos)
357 pos2 = line.find_first_not_of (sep, pos2);
358 if (pos2 != std::string::npos)
361 pos2 = line.length () - 1;
366 if (pos2 != std::string::npos)
369 pos1 = std::string::npos;
372 while (pos1 != std::string::npos);
375 cdata.
resize (rmax, cmax);
377 rdata.
resize (rmax, cmax);
380 r = (r > i + 1 ? r : i + 1);
383 size_t pos1 = line.find_first_not_of (
" \t");
388 size_t pos2 = line.find_first_of (sep, pos1);
389 std::string str = line.substr (pos1, pos2 - pos1);
391 if (sepflag && pos2 != std::string::npos)
393 pos2 = line.find_first_not_of (sep, pos2) - 1;
395 c = (c > j + 1 ? c : j + 1);
396 if (r > rmax || c > cmax)
403 cdata.
resize (rmax, cmax);
405 rdata.
resize (rmax, cmax);
408 tmp_stream.str (str);
414 if (tmp_stream.eof ())
421 else if (std::toupper (tmp_stream.peek ()) ==
'I')
433 if (!iscmplx && y != 0.)
446 cdata(i,j++) = empty_value;
448 rdata(i,j++) = empty_value;
450 if (pos2 != std::string::npos)
453 pos1 = std::string::npos;
456 while (pos1 != std::string::npos);